Created
March 29, 2016 17:41
-
-
Save robperc/efc7dd5e8a053a25ad5c944661d758fb to your computer and use it in GitHub Desktop.
Editing of Login Items in user context using PyObjC
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
import sys | |
import Cocoa | |
import CoreFoundation | |
from LaunchServices import LSSharedFileListCreate, kLSSharedFileListSessionLoginItems, LSSharedFileListCopySnapshot, kLSSharedFileListItemBeforeFirst, LSSharedFileListItemCopyDisplayName, LSSharedFileListInsertItemURL | |
TO_ADD_NAME = "YourAppName" | |
TO_ADD_PATH = "/Applications/YourAppname.app/" | |
items = LSSharedFileListCreate(CoreFoundation.kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None) | |
snapshot = LSSharedFileListCopySnapshot(items, None) | |
names = [LSSharedFileListItemCopyDisplayName(item) for item in snapshot[0]] | |
if TO_ADD_NAME in names: | |
# Item is already a login item... | |
sys.exit() | |
item = Cocoa.NSURL.alloc().init() | |
name = Cocoa.NSString.alloc().initWithString_(TO_ADD_NAME) | |
path = "file://localhost%s" % (TO_ADD_PATH) | |
url = Cocoa.NSString.alloc().initWithString_(path) | |
item = Cocoa.NSURL.URLWithString_(url.stringByAddingPercentEscapesUsingEncoding_(Cocoa.NSASCIIStringEncoding)) | |
LSSharedFileListInsertItemURL(items, kLSSharedFileListItemBeforeFirst, name, None, item, None, None) | |
CoreFoundation.CFPreferencesSynchronize(CoreFoundation.kCFPreferencesAnyApplication,CoreFoundation.kCFPreferencesCurrentUser,CoreFoundation.kCFPreferencesCurrentHost) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment