Last active
December 16, 2015 10:59
-
-
Save rufferto/5424520 to your computer and use it in GitHub Desktop.
Add and delete global login items for Mac 10.6+. ARC code. C routines.
This file contains hidden or 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
#import <ApplicationServices/ApplicationServices.h> | |
void addLoginItem(NSURL *appURL) | |
{ | |
AuthorizationItem right[1] = {{"system.global-login-items.", 0, NULL, 0}}; | |
AuthorizationRights setOfRights = {1, right}; | |
AuthorizationRef auth = NULL; | |
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth); | |
if (auth != NULL) | |
{ | |
AuthorizationCopyRights(auth, &setOfRights, kAuthorizationEmptyEnvironment, | |
(kAuthorizationFlagDefaults | |
| kAuthorizationFlagInteractionAllowed | |
| kAuthorizationFlagExtendRights), NULL); | |
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListGlobalLoginItems, NULL); // or kLSSharedFileListSessionLoginItems | |
LSSharedFileListSetAuthorization(loginItems, auth); | |
if (loginItems != NULL) | |
{ | |
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemLast, | |
NULL, NULL, (__bridge CFURLRef)(appURL), NULL, NULL); | |
if (item) | |
CFRelease(item); | |
CFRelease(loginItems); | |
} | |
AuthorizationFree(auth, kAuthorizationFlagDefaults); | |
} | |
} | |
void deleteLoginItem(NSURL *forURL) | |
{ | |
AuthorizationItem right[1] = {{"system.global-login-items.", 0, NULL, 0}}; | |
AuthorizationRights setOfRights = {1, right}; | |
AuthorizationRef auth = NULL; | |
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth); | |
if (auth != NULL) | |
{ | |
AuthorizationCopyRights(auth, &setOfRights, kAuthorizationEmptyEnvironment, | |
(kAuthorizationFlagDefaults | |
| kAuthorizationFlagInteractionAllowed | |
| kAuthorizationFlagExtendRights), NULL); | |
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListGlobalLoginItems, NULL); // or kLSSharedFileListSessionLoginItems | |
LSSharedFileListSetAuthorization(loginItems, auth); | |
if (loginItems) | |
{ | |
UInt32 seedValue; | |
NSArray *loginItemsArray = (NSArray *)CFBridgingRelease(LSSharedFileListCopySnapshot(loginItems, &seedValue)); | |
NSUInteger count = [loginItemsArray count]; | |
NSUInteger i; | |
for (i = 0; i < count; i++) | |
{ | |
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)([loginItemsArray objectAtIndex:i]); | |
CFURLRef url = NULL;; | |
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes; | |
if (LSSharedFileListItemResolve(itemRef, resolutionFlags, &url, NULL) == noErr) | |
{ | |
NSString *urlPath = CFBridgingRelease(CFURLCopyPath(url)); | |
if ([[forURL path] isEqualToString:urlPath]) | |
LSSharedFileListItemRemove(loginItems, itemRef); | |
CFRelease(url); | |
} | |
} | |
CFRelease(loginItems); | |
} | |
AuthorizationFree(auth, kAuthorizationFlagDefaults); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment