PDA

View Full Version : Automatically start applications on login




cineologist
Oct 19, 2009, 12:14 PM
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



chown33
Oct 19, 2009, 12:41 PM
Customizing Login and Logout:
http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html

Google search terms:
login items site:developer.apple.com

Darkroom
Oct 19, 2009, 01:14 PM
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)
{
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

cineologist
Oct 19, 2009, 07:48 PM
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!

cineologist
Oct 20, 2009, 03:55 PM
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)
{
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:


- (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!