-
-
Save bibhas/455e471daa430062857e3359d268fd53 to your computer and use it in GitHub Desktop.
Add app to Login item. http://cocoatutorial.grapewave.com/tag/mac-os-x/
+ me
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void) addAppAsLoginItem { | |
if ([self wasAppAddedAsLoginItem]) return; | |
NSString * appPath = [[NSBundle mainBundle] bundlePath]; | |
// This will retrieve the path for the application | |
// For example, /Applications/test.app | |
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath]; | |
// Create a reference to the shared file list. | |
// We are adding it to the current user only. | |
// If we want to add it all users, use | |
// kLSSharedFileListGlobalLoginItems instead of | |
//kLSSharedFileListSessionLoginItems | |
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); | |
if (loginItems) { | |
//Insert an item to the list. | |
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, | |
kLSSharedFileListItemLast, NULL, NULL, | |
url, NULL, NULL); | |
if (item){ | |
CFRelease(item); | |
} | |
CFRelease(loginItems); | |
} | |
} | |
- (BOOL)wasAppAddedAsLoginItem { | |
NSString * appPath = [[NSBundle mainBundle] bundlePath]; | |
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath]; | |
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); | |
BOOL ret = NO; | |
if (loginItems) { | |
UInt32 seedValue; | |
//Retrieve the list of Login Items and cast them to | |
// a NSArray so that it will be easier to iterate. | |
CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue); | |
for (id item in (__bridge NSArray *)loginItemsArray) { | |
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item; | |
//Resolve the item with URL | |
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) { | |
NSString * urlPath = [(__bridge NSURL*)url path]; | |
if ([urlPath compare:appPath] == NSOrderedSame){ | |
ret = YES; | |
} | |
} | |
} | |
CFRelease(loginItemsArray); | |
CFRelease(loginItems); | |
} | |
return ret; | |
} | |
-(void) deleteAppFromLoginItem { | |
NSString * appPath = [[NSBundle mainBundle] bundlePath]; | |
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath]; | |
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); | |
if (loginItems) { | |
UInt32 seedValue; | |
CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue); | |
for (id item in (__bridge NSArray *)loginItemsArray) { | |
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item; | |
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) { | |
NSString * urlPath = [(__bridge NSURL*)url path]; | |
if ([urlPath compare:appPath] == NSOrderedSame){ | |
LSSharedFileListItemRemove(loginItems, itemRef); | |
} | |
} | |
} | |
CFRelease(loginItemsArray); | |
CFRelease(loginItems); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment