来自 launchd 的 NSAppleScript

发布于 2024-12-20 23:45:36 字数 503 浏览 1 评论 0原文

我有一个 launchd 服务和一个 Cocoa 应用程序。当用户从应用程序文件夹中单击该应用程序时,该应用程序就会运行。我想从服务执行以下 Apple 脚本。但即使在 xcode 的调试版本中似乎可以工作,但当它作为 launchd 服务构建和安装时,它就不起作用。

NSString* path = @"/Applications/Sample.app/Contents/Resources/reset.scpt";
NSURL* url = [NSURL fileURLWithPath:path];
NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = 
    [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
[appleScript release];

I have a launchd service and a Cocoa app. The app runs when the user clicks on it from the application folder. I want to execute the following Apple Script from the service. But even though appears to work in the debug version in xcode, it does not work when it is built and installed as launchd service.

NSString* path = @"/Applications/Sample.app/Contents/Resources/reset.scpt";
NSURL* url = [NSURL fileURLWithPath:path];
NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = 
    [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
[appleScript release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-12-27 23:45:36

我假设您的 launchd .plist 文件有问题。

launchd 的一个常见错误是:launchd 不是 shell
当尝试使用 launchd 执行 Apple 脚本时,尝试将其传递给 shell:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>YOUR.LABEL</string>

        <key>ProgramArguments</key>
        <array>
            <string>/bin/zsh</string>
            <string>-c</string>
            <string>path/to/your/apple/script</string>
        </array>
    </dict>
</plist>

.plist 放入 ~/Library/LaunchAgents/YOUR.LABEL.plist (文件名有与 Label 值相对应)

然后加载 plist 文件:

launchctl enable gui/"$(id -u)"/YOUR.LABEL

MacOS 缓存 plist 文件。因此,如果您已经有代理的 plist 文件:注销并再次登录以刷新缓存。

现在启动代理:

launchctl kickstart gui/"$(id -u)"/YOUR.LABEL

请参阅:https://launchd.info/ 了解更多信息。

I'm assuming there is a problem with your launchd .plist file.

A common mistake with launchd is: launchd is not shell.
When trying to execute the apple script with launchd try to pass it to shell:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>YOUR.LABEL</string>

        <key>ProgramArguments</key>
        <array>
            <string>/bin/zsh</string>
            <string>-c</string>
            <string>path/to/your/apple/script</string>
        </array>
    </dict>
</plist>

Place the .plist in ~/Library/LaunchAgents/YOUR.LABEL.plist (The filename has to correspond with the Label value)

Then load the plist file:

launchctl enable gui/"$(id -u)"/YOUR.LABEL

MacOS caches the plist file. So if you already have a plist file for your agent: Logout and login again to refresh the cache.

Now start the agent:

launchctl kickstart gui/"$(id -u)"/YOUR.LABEL

See: https://launchd.info/ for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文