I need a way to add an application to the Login Items from a postflight script which is run as part of my installer. It needs to work on 10.5+. Preferably, it would work in a bash script. My application already requires administrative rights.
The approach I found here: https://stackoverflow.com/questions/4912212/mac-os-login-items-with-arguments seemed to be on the right track (included below)... but didn't work when I tried it on command line and I'm not sure how to make it install for All Users or if I need to add logic to check if it's already added to startup items before calling this code.
#!/bin/bash
/usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"$1\", hidden:false } at end"
I suspect I could also do something with a launchd. But, I'm not sure which approach is the best practice for compatibility across versions.
If you don't mind a bit of reading, I'd suggest starting with Apple Technical Note TN2083: Daemons and Agents.
I haven't tested it, but I believe the easiest way to do what you want is via a
launchd
agent. That essentially involves dropping aplist
file in/Library/LaunchAgents
. A nice side benefit of this is that you could overwrite the same file there as many times as you like, and you shouldn't get multiple instances of your item (per user).Something like:
The other possibility is a global login item. From that technical note mentioned above, I gleaned:
System Events
process. [So perhaps this no longer works reliably as of 10.5?]I believe that a lot of applications (prior to
launchd
in 10.5) used to manipulate theloginwindow.plist
file directly. Unfortunately, a lot of them did it incorrectly – I can't tell you how many half-XML-half-binary-all-brokenloginwindow.plist
files I've seen.Fission is pretty spot on.
There are some additional details on adding to login items from bash in the following stackoverflow post:
https://stackoverflow.com/questions/6947925/add-app-to-osx-login-items-during-a-package-maker-installer-postflight-script/7643260#7643260