-
-
Save mlzxy/a0e663a1eb186df74cae to your computer and use it in GitHub Desktop.
Swift / OS X: Launch app at startup
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
// Adding Login Items Using a Shared File List | |
// This is a combination of the code provided by the following Stackoverflow discussion | |
// http://stackoverflow.com/questions/26475008/swift-getting-a-mac-app-to-launch-on-startup | |
// (This approach will not work with App-Sandboxing.) | |
func applicationIsInStartUpItems() -> Bool { | |
return itemReferencesInLoginItems().existingReference != nil | |
} | |
func toggleLaunchAtStartup() { | |
let itemReferences = itemReferencesInLoginItems() | |
let shouldBeToggled = (itemReferences.existingReference == nil) | |
let loginItemsRef = LSSharedFileListCreate( | |
nil, | |
kLSSharedFileListSessionLoginItems.takeRetainedValue(), | |
nil | |
).takeRetainedValue() as LSSharedFileListRef? | |
if loginItemsRef != nil { | |
if shouldBeToggled { | |
if let appUrl: CFURLRef = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) { | |
LSSharedFileListInsertItemURL(loginItemsRef, itemReferences.lastReference, nil, nil, appUrl, nil, nil) | |
println("Application was added to login items") | |
} | |
} else { | |
if let itemRef = itemReferences.existingReference { | |
LSSharedFileListItemRemove(loginItemsRef,itemRef); | |
println("Application was removed from login items") | |
} | |
} | |
} | |
} | |
func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItemRef?, lastReference: LSSharedFileListItemRef?) { | |
var itemUrl = UnsafeMutablePointer<Unmanaged<CFURL>?>.alloc(1) | |
if let appUrl = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) { | |
let loginItemsRef = LSSharedFileListCreate( | |
nil, | |
kLSSharedFileListSessionLoginItems.takeRetainedValue(), | |
nil | |
).takeRetainedValue() as LSSharedFileListRef? | |
if loginItemsRef != nil { | |
let loginItems = LSSharedFileListCopySnapshot(loginItemsRef, nil).takeRetainedValue() as NSArray | |
println("There are \(loginItems.count) login items") | |
if(loginItems.count > 0) { | |
let lastItemRef = loginItems.lastObject as! LSSharedFileListItemRef | |
for var i = 0; i < loginItems.count; ++i { | |
let currentItemRef = loginItems.objectAtIndex(i) as! LSSharedFileListItemRef | |
if LSSharedFileListItemResolve(currentItemRef, 0, itemUrl, nil) == noErr { | |
if let urlRef: NSURL = itemUrl.memory?.takeRetainedValue() { | |
println("URL Ref: \(urlRef.lastPathComponent)") | |
if urlRef.isEqual(appUrl) { | |
return (currentItemRef, lastItemRef) | |
} | |
} | |
} | |
else { | |
println("Unknown login application") | |
} | |
} | |
// The application was not found in the startup list | |
return (nil, lastItemRef) | |
} else { | |
let addatstart: LSSharedFileListItemRef = kLSSharedFileListItemBeforeFirst.takeRetainedValue() | |
return(nil,addatstart) | |
} | |
} | |
} | |
return (nil, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, looks like methods for Login Items is deprecated now... So, the only way is to use Service Management Framework?