Last active
December 22, 2018 00:50
-
-
Save RGreinacher/5fde649e163b705633e7 to your computer and use it in GitHub Desktop.
Revisions
-
RGreinacher renamed this gist
Jul 14, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
RGreinacher revised this gist
Jun 7, 2015 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,7 @@ // 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 -
RGreinacher created this gist
Jun 6, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ // http://stackoverflow.com/questions/26475008/swift-getting-a-mac-app-to-launch-on-startup 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) }