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.
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.
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"]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With