Automatically start applications on login

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

  1. macrumors newbie

    Joined:
    Dec 30, 2008
    #1
    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.

    Chad
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
  3. Guest

    Darkroom

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

    Code:
    @implementation EnableLogin (PrivateMethods)
    
    - (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath
    	{
    	LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(theLoginItemsRefs, kLSSharedFileListItemLast, NULL, NULL, thePath, NULL, NULL);		
    	if (item)
    		{
    		CFRelease(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];
    	}
    
    @end
    
     
  4. thread starter macrumors newbie

    Joined:
    Dec 30, 2008
    #4
    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

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

    Code:
    - (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];
    		else
    			[self disableLoginItemWithLoginItemsReference:loginItems ForPath:url];
    	}
    	CFRelease(loginItems);
    }
    
    Thanks everybody for pitching in!
     

Share This Page