开发者

LaunchAgents for GUI app

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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜