Created
February 14, 2016 15:20
-
-
Save burnsra/e7e8431a444aa45768ce to your computer and use it in GitHub Desktop.
A helper to add an application to a users login items
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
// | |
// LoginHelper.swift | |
// SquidBar | |
// | |
// Created by Robert Burns on 2/11/16. | |
// Copyright © 2016 Robert Burns. All rights reserved. | |
// | |
import Foundation | |
func applicationIsInStartUpItems() -> Bool { | |
return itemReferencesInLoginItems().existingReference != nil | |
} | |
func setLaunchAtStartup(launchOnStartup: Bool) { | |
let itemReferences = itemReferencesInLoginItems() | |
let isNotAlreadySet = (itemReferences.existingReference == nil) | |
let loginItemsRef = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() as LSSharedFileListRef? | |
if loginItemsRef != nil { | |
if isNotAlreadySet && launchOnStartup { | |
if let appUrl: CFURLRef = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) { | |
LSSharedFileListInsertItemURL(loginItemsRef, itemReferences.lastReference, nil, nil, appUrl, nil, nil) | |
} | |
} else if !isNotAlreadySet && !launchOnStartup { | |
if let itemRef = itemReferences.existingReference { | |
LSSharedFileListItemRemove(loginItemsRef,itemRef); | |
} | |
} | |
} | |
} | |
func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItemRef?, lastReference: LSSharedFileListItemRef?) { | |
if let appUrl : NSURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) { | |
let loginItemsRef = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() as LSSharedFileListRef? | |
if loginItemsRef != nil { | |
let loginItems: NSArray = LSSharedFileListCopySnapshot(loginItemsRef, nil).takeRetainedValue() as NSArray | |
if(loginItems.count > 0) | |
{ | |
let lastItemRef: LSSharedFileListItemRef = loginItems.lastObject as! LSSharedFileListItemRef | |
for var i = 0; i < loginItems.count; ++i { | |
let currentItemRef: LSSharedFileListItemRef = loginItems.objectAtIndex(i) as! LSSharedFileListItemRef | |
if let itemURL = LSSharedFileListItemCopyResolvedURL(currentItemRef, 0, nil) { | |
if (itemURL.takeRetainedValue() as NSURL).isEqual(appUrl) { | |
return (currentItemRef, lastItemRef) | |
} | |
} | |
} | |
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