Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (60 sloc) 2.66 KB
//
// JPLaunchAtLoginManager.m
// CloudyTabs
//
// Based on a modified version of this StackOverflow answer - http://stackoverflow.com/a/2318004/446039
//
#import "JPLaunchAtLoginManager.h"
@implementation JPLaunchAtLoginManager
+ (BOOL)willStartAtLogin:(NSURL *)itemURL
{
Boolean foundIt = false;
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seed = 0U;
NSArray *currentLoginItems = (__bridge NSArray *)(LSSharedFileListCopySnapshot(loginItems, &seed));
for (id itemObject in currentLoginItems) {
LSSharedFileListItemRef item = (__bridge LSSharedFileListItemRef)itemObject;
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef URL = NULL;
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, NULL);
if (err == noErr) {
foundIt = CFEqual(URL, (__bridge CFTypeRef)(itemURL));
CFRelease(URL);
if (foundIt)
break;
}
}
CFRelease(loginItems);
}
return (BOOL)foundIt;
}
+ (void)setStartAtLogin:(NSURL *)itemURL enabled:(BOOL)enabled
{
LSSharedFileListItemRef existingItem = NULL;
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seed = 0U;
NSArray *currentLoginItems = (__bridge NSArray *)(LSSharedFileListCopySnapshot(loginItems, &seed));
for (id itemObject in currentLoginItems) {
LSSharedFileListItemRef item = (__bridge LSSharedFileListItemRef)itemObject;
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef URL = NULL;
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, NULL);
if (err == noErr) {
Boolean foundIt = CFEqual(URL, (__bridge CFTypeRef)(itemURL));
CFRelease(URL);
if (foundIt) {
existingItem = item;
break;
}
}
}
if (enabled && (existingItem == NULL)) {
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, (__bridge CFURLRef)itemURL, NULL, NULL);
} else if (!enabled && (existingItem != NULL))
LSSharedFileListItemRemove(loginItems, existingItem);
CFRelease(loginItems);
}
}
@end