Programatically adding Applications to the Login Items

Its taking some kicking around, but here is the fruit of my labour… using core foundation over the RC bridge.

require 'osx/cocoa'

loginItems = OSX::CFPreferencesCopyValue(
      "AutoLaunchedApplicationDictionary", 
      "loginwindow", 
      OSX::KCFPreferencesCurrentUser, 
      OSX::KCFPreferencesAnyHost)

application_path = File.expand_path("~/path/to/your.app")
application_hidden = false

loginItems << OSX::NSDictionary.dictionaryWithObjects_forKeys(
    [ application_path, application_hidden ], 
    [ :Path, :Hide ])

# puts loginItems

OSX::CFPreferencesSetValue(
    "AutoLaunchedApplicationDictionary", 
    loginItems, 
    "loginwindow", 
    OSX::KCFPreferencesCurrentUser, 
    OSX::KCFPreferencesAnyHost)

OSX::CFPreferencesSynchronize("loginwindow", 
        OSX::KCFPreferencesCurrentUser, 
        OSX::KCFPreferencesAnyHost)

With any luck this might help someone :-)

comments powered by Disqus