GUI 应用程序的启动代理

发布于 2024-09-24 13:03:26 字数 863 浏览 5 评论 0原文

我想在每次用户登录时启动我的应用程序。

我将 plist 文件添加到 /Libray/LaunchAgents 文件夹:

<?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>KeepAlive</key>
 <false/>
 <key> LaunchOnlyOnce</key>
 <true/>
 <key>OnDemand</key>
 <false/>
 <key>RunAtLoad</key>
 <true/>
 <key>Label</key>
 <string>com.mycompany.myapp</string>
 <key>ProgramArguments</key>
 <array>
  <string>/Applications/mayapp.app/Contents/MacOS/myapp</string>
 </array>
</dict>
</plist>

一切看起来都正常,正在加载应用程序,但是当我退出应用程序时,它会由 launchd 服务重新启动。

我应该在 plist 文件中添加/修改哪个键,以防止不断重新启动我的应用程序。

I would like to launch my app each time user logs-in.

I added plist file to /Libray/LaunchAgents folder :

<?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>KeepAlive</key>
 <false/>
 <key> LaunchOnlyOnce</key>
 <true/>
 <key>OnDemand</key>
 <false/>
 <key>RunAtLoad</key>
 <true/>
 <key>Label</key>
 <string>com.mycompany.myapp</string>
 <key>ProgramArguments</key>
 <array>
  <string>/Applications/mayapp.app/Contents/MacOS/myapp</string>
 </array>
</dict>
</plist>

All looks OK, application is being loaded , however when I quit my app it's launch back by launchd service.

Which key shall I add/modify in my plist file to prevent constant relaunching of my application.

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

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

发布评论

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

评论(3

你怎么敢 2024-10-01 13:03:26

如果您想在登录时启动常规应用程序,我建议使用 LaunchServices 共享文件列表 API 而不是 launchd。您无需安装 launchd plist,只需使用此 API 将应用程序添加到用户的登录项(您在系统偏好设置的帐户首选项窗格中看到的登录项)即可。这样做的优点是:a) 对于用户来说,应用程序在登录时启动的原因更加明显,b) 用户更容易删除它,c) 如果用户删除您的应用程序,launchd 会向控制台抱怨错误当它无法启动(现在丢失)应用程序时。

该 API 似乎没有任何参考文档,但相关函数可在 LSSharedFileList.h 中找到。代码如下所示:

#import <CoreServices/CoreServices.h>

...

LSSharedFileListRef loginItemList = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, NULL);
if (loginItemList != NULL)
{
    LSSharedFileListRef myItem = LSSharedFileListInsertItemURL(loginItemList, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)[[NSBundle mainBundle] bundleURL], NULL, NULL);
    //We don't do anything with the new item, but we need to release it so it doesn't leak
    if (myItem != NULL)
        CFRelease(myItem);
    CFRelease(loginItemList);
}

如果您想让此项目为所有用户启动,而不仅仅是当前登录的用户在用户中,您可以使用 kLSSharedFileListGlobalLoginItems 而不是 kLSSharedFileListSessionLoginItems。

If you want to launch a regular application at login, I would recommend using the LaunchServices shared file list API rather than launchd. Rather than having to install a launchd plist, you can just use this API to add your application to the user's login items (the ones you see in the Accounts pref pane in System Preferences). The advantages to this are a) it's more obvious to the user why the application is launching at login, b) it's easier for the user to remove it, and c) if the user deletes your application, launchd will complain with errors to the console when it fails to launch the (now missing) application.

There doesn't appear to be any reference documentation for the API, but the relevant functions are found in LSSharedFileList.h The code for this would look something like:

#import <CoreServices/CoreServices.h>

...

LSSharedFileListRef loginItemList = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, NULL);
if (loginItemList != NULL)
{
    LSSharedFileListRef myItem = LSSharedFileListInsertItemURL(loginItemList, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)[[NSBundle mainBundle] bundleURL], NULL, NULL);
    //We don't do anything with the new item, but we need to release it so it doesn't leak
    if (myItem != NULL)
        CFRelease(myItem);
    CFRelease(loginItemList);
}

If you want to have this item launch for all users rather than just the currently logged in user, you can use kLSSharedFileListGlobalLoginItems instead of kLSSharedFileListSessionLoginItems.

圈圈圆圆圈圈 2024-10-01 13:03:26

我看到两个问题:第一个是您有 OnDemand,它告诉 launchd 代理需要我保持活动状态(并且出现覆盖 KeepAlive,这意味着完全相反)。第二个问题是 中的键名前面有一个空格。 LaunchOnlyOnce。简单的解决方案:删除 OnDemand 和 LaunchOnlyOnce 键,它应该可以正常工作。

I see two problems: the primary one is that you have <key>OnDemand</key><false/>, which tells launchd that the agent needs to me kept alive (and that appears to be overriding <key>KeepAlive</key><false/>, which means exactly the opposite). The secondary problem is that you have a space before the key name in <key> LaunchOnlyOnce</key><true/>. Simple solution: remove both the OnDemand and LaunchOnlyOnce keys, and it should work fine.

残疾 2024-10-01 13:03:26

删除“保持活动”键并仅启动一次键...因为您只需要启动应用程序一次。以下是启动名为登录应用程序的应用程序的示例代码。

<?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>RunAtLoad</key>
<true/>
<key>Label</key>
<string>com.apple.LoginApp</string>
<key>Program</key>
<string>/Library/Log Files/LoginApp.app/Contents/MacOS/LoginApp</string>
<key>onDemand</key>
<false/>
</dict>
</plist>

希望这有帮助

remove the Keep Alive key AND launch only once key... since u require to launch the app only once. Here is a sample code to launch an app named as login app.

<?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>RunAtLoad</key>
<true/>
<key>Label</key>
<string>com.apple.LoginApp</string>
<key>Program</key>
<string>/Library/Log Files/LoginApp.app/Contents/MacOS/LoginApp</string>
<key>onDemand</key>
<false/>
</dict>
</plist>

Hope this helps

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