Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cineologist

macrumors newbie
Original poster
Dec 30, 2008
4
0
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
 
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
 
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!
 
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

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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.