Created
May 23, 2015 23:45
-
-
Save plapier/f8e1dde1b1624dfbb3e4 to your computer and use it in GitHub Desktop.
Add Login item using Launch Services (Shared List File) in Swift
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 characters
// | |
// toggleLaunchAtStartup.swift | |
// Recents | |
// | |
// Created by Phil LaPier on 5/21/15. | |
// Copyright (c) 2015 Philip LaPier. All rights reserved. | |
// | |
// With help from: https://github.com/RamonGilabert/Prodam/blob/master/Prodam/Prodam/LaunchStarter.swift | |
import Foundation | |
func applicationIsInStartUpItems() -> Bool { | |
return (itemReferencesInLoginItems().existingReference != nil) | |
} | |
func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItemRef?, lastReference: LSSharedFileListItemRef?) { | |
if let appURL : NSURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) { | |
if let loginItemsRef = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() as LSSharedFileListRef? { | |
let loginItems: NSArray = LSSharedFileListCopySnapshot(loginItemsRef, nil).takeRetainedValue() as NSArray | |
let lastItemRef: LSSharedFileListItemRef = loginItems.lastObject as! LSSharedFileListItemRef | |
for (index, loginItem) in enumerate(loginItems) { | |
let currentItemRef: LSSharedFileListItemRef = loginItems.objectAtIndex(index) as! LSSharedFileListItemRef | |
if let itemURL = LSSharedFileListItemCopyResolvedURL(currentItemRef, 0, nil) { | |
if (itemURL.takeRetainedValue() as NSURL).isEqual(appURL) { | |
return (currentItemRef, lastItemRef) | |
} | |
} | |
} | |
return (nil, lastItemRef) | |
} | |
} | |
return (nil, nil) | |
} | |
func toggleLaunchAtStartup() { | |
let itemReferences = itemReferencesInLoginItems() | |
let shouldBeToggled = (itemReferences.existingReference == nil) | |
if let loginItemsRef = LSSharedFileListCreate( nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() as LSSharedFileListRef? { | |
if shouldBeToggled { | |
if let appUrl : CFURLRef = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) { | |
println("Add login item") | |
LSSharedFileListInsertItemURL(loginItemsRef, itemReferences.lastReference, nil, nil, appUrl, nil, nil) | |
} | |
} else { | |
if let itemRef = itemReferences.existingReference { | |
println("Remove login item") | |
LSSharedFileListItemRemove(loginItemsRef,itemRef); | |
} | |
} | |
} | |
} |
Would you mind update to Swift 4?
anyone know of a LSSharedFileListCreate
replacement ?
@andrewcrook It appears LaunchServices now has a new API called SMAppService. More instructions here for similar functionality 👉 https://nilcoalescing.com/blog/LaunchAtLoginSetting/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to swift 3