views:

51

answers:

2

Hi 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"&gt;
<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.

Thanks David

A: 

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.

Brian Webster
A: 

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.

Gordon Davisson
Thanks to Gordon and Brian ,
David
LSSharedFileListRef perhaps is better controled but it is not supported on 10.4 and my app has to run on 10.4 . I fixed plist according to Gordon's procedure and it works , however application is not launched for all users on the same machine. If I Fast User Switch new session does not launch my app....
David
Is there any workaround? Thanks David
David