Skip to content

Instantly share code, notes, and snippets.

@MaddTheSane
Forked from boyvanamstel/PreferencesManager.c
Last active April 15, 2019 03:32
Show Gist options
  • Save MaddTheSane/e73dbdad1e214f68ee95b5926fb23b46 to your computer and use it in GitHub Desktop.
Save MaddTheSane/e73dbdad1e214f68ee95b5926fb23b46 to your computer and use it in GitHub Desktop.
Objective-C class to loop through startup items with Automatic Reference Counting (ARC) enabled
// Based on http://www.bdunagan.com/2010/09/25/cocoa-tip-enabling-launch-on-startup/
// Almost automatic convert to ARC, might need tweaking.
#import "PreferencesManager.h"
@implementation PreferencesManager
// MIT license
- (BOOL)isLaunchAtStartup {
// See if the app is currently in LoginItems.
LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
// Store away that boolean.
BOOL isInList = itemRef != nil;
// Release the reference if it exists.
if (itemRef != nil) CFRelease(itemRef);
return isInList;
}
- (BOOL)toggleLaunchAtStartup {
// Toggle the state.
BOOL shouldBeToggled = ![self isLaunchAtStartup];
// Get the LoginItems list.
LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItemsRef == nil) return shouldBeToggled;
if (shouldBeToggled) {
// Add the app to the LoginItems list.
CFURLRef appUrl = (__bridge CFURLRef)[[NSBundle mainBundle] bundleURL];
LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
if (itemRef) CFRelease(itemRef);
}
else {
// Remove the app from the LoginItems list.
LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
LSSharedFileListItemRemove(loginItemsRef,itemRef);
if (itemRef != nil) CFRelease(itemRef);
}
return shouldBeToggled;
}
- (LSSharedFileListItemRef)itemRefInLoginItems {
LSSharedFileListItemRef itemRef = nil;
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.
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seedValue;
//Retrieve the list of Login Items and cast them to
// a NSArray so that it will be easier to iterate.
NSArray *loginItemsArray = CFBridgingRelease(LSSharedFileListCopySnapshot(loginItems, &seedValue));
for(int i = 0; i< [loginItemsArray count]; i++){
LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItemsArray
objectAtIndex:i];
//Resolve the item with URL
if (LSSharedFileListItemResolve(currentItemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
NSString * urlPath = [(__bridge NSURL*)url path];
if ([urlPath compare:appPath] == NSOrderedSame){
itemRef = currentItemRef;
}
}
}
}
CFRelease(loginItems);
return itemRef;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment