Automatically start applications on login

Discussion in 'Mac Programming' started by cineologist, Oct 19, 2009.

  1. macrumors newbie

    Dec 30, 2008
    Hello fellow developers,

    I have developed an application using Objective-C using Xcode. I'm ready to release the software except for one last hurdle I need to figure out and that is to automatically start the application when user logs into their account.

    I am aware that System Preference has login items that can be added.

    What I am interested in is to develop a script that would add the application to the start up list during the installation procedure.

    Any tips are greatly appreciated.

  2. macrumors 604

    Aug 9, 2009
  3. Guest


    Dec 15, 2006
    Montréal, Canada
    this works with OS 10.5, but i'm not sure about 10.6. it doesn't work with 10.4.

    @implementation EnableLogin (PrivateMethods)
    - (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath
    	LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(theLoginItemsRefs, kLSSharedFileListItemLast, NULL, NULL, thePath, NULL, NULL);		
    	if (item)
    - (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath
    	UInt32 seedValue;
    	NSArray  *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(theLoginItemsRefs, &seedValue);
    	for (id item in loginItemsArray)
    		LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
    		if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr)
    			if ([[(NSURL *)thePath path] hasPrefix:[[NSBundle mainBundle] bundlePath]])
    			LSSharedFileListItemRemove(theLoginItemsRefs, itemRef);
    	[loginItemsArray release];
  4. thread starter macrumors newbie

    Dec 30, 2008
    Many thanks! I know for a fact that future developers who comes across this will definitely appreciate this a lot, including myself.

    That helped a lot!
  5. thread starter macrumors newbie

    Dec 30, 2008
    Thought this would help others who need to know what to do to use the EnableLogin object:

    - (IBAction)addLoginItem:(id)sender {
    	NSString* applicationPath = [[NSBundle mainBundle] bundlePath];
    	CFURLRef url = (CFURLRef)[NSURL fileURLWithPath: applicationPath];
    	// Create a reference to the shared file list.
    	LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    	if (loginItems) {
    		if ([[checkStartup selectedCell] state] == YES) //checkStartup should be you checkbox that adds/removes the item from the list
    			[self enableLoginItemWithLoginItemsReference:loginItems ForPath:url];
    			[self disableLoginItemWithLoginItemsReference:loginItems ForPath:url];
    Thanks everybody for pitching in!

Share This Page