Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make your App open at login? [duplicate]

Tags:

cocoa

login

Just wondering how I can make my app open automatically at login, but make this be able to be toggled on and off using a check box in the preferences window.

like image 443
Joshua Avatar asked May 02 '09 15:05

Joshua


People also ask

How do I open two apps at the same time on my computer?

Select the Task View icon on the taskbar, or press Alt-Tab on your keyboard to see apps or switch between them. To use two or more apps at a time, grab the top of an app window and drag it to the side. Then choose another app and it'll automatically snap into place.


1 Answers

Here's some code that I use, it's based on the Growl source.

+ (BOOL) willStartAtLogin:(NSURL *)itemURL {     Boolean foundIt=false;     LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);     if (loginItems) {         UInt32 seed = 0U;         NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];         for (id itemObject in currentLoginItems) {             LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;              UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;             CFURLRef URL = NULL;             OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, /*outRef*/ NULL);             if (err == noErr) {                 foundIt = CFEqual(URL, itemURL);                 CFRelease(URL);                  if (foundIt)                     break;             }         }         CFRelease(loginItems);     }     return (BOOL)foundIt; }  + (void) setStartAtLogin:(NSURL *)itemURL enabled:(BOOL)enabled {     OSStatus status;     LSSharedFileListItemRef existingItem = NULL;      LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);     if (loginItems) {         UInt32 seed = 0U;         NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];         for (id itemObject in currentLoginItems) {             LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;              UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;             CFURLRef URL = NULL;             OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, /*outRef*/ NULL);             if (err == noErr) {                 Boolean foundIt = CFEqual(URL, itemURL);                 CFRelease(URL);                  if (foundIt) {                     existingItem = item;                     break;                 }             }         }          if (enabled && (existingItem == NULL)) {             LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst,                                           NULL, NULL, (CFURLRef)itemURL, NULL, NULL);          } else if (!enabled && (existingItem != NULL))             LSSharedFileListItemRemove(loginItems, existingItem);          CFRelease(loginItems);     }        } 

If you want an easy to implement checkbox, make a @property BOOL startAtLogin; in one of your classes and implement it as follows. Just bind the checkbox value to the property and it should all work seamlessly.

- (NSURL *)appURL {     return [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; }  - (BOOL)startAtLogin {     return [LoginItem willStartAtLogin:[self appURL]]; }  - (void)setStartAtLogin:(BOOL)enabled {     [self willChangeValueForKey:@"startAtLogin"];     [LoginItem setStartAtLogin:[self appURL] enabled:enabled];     [self didChangeValueForKey:@"startAtLogin"]; } 
like image 62
Nick Moore Avatar answered Sep 18 '22 16:09

Nick Moore