Skip to content

Instantly share code, notes, and snippets.

@rufferto
Last active December 16, 2015 10:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rufferto/5424520 to your computer and use it in GitHub Desktop.
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.
#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