Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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: http://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.

share|improve this question
add comment

2 Answers 2

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 a plist 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:

    #!/bin/sh
    cat > /Library/LaunchAgents/com.serverfault.myapp.plist << EOT
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
            <key>Label</key>
            <string>com.serverfault.myapp</string>
            <key>ProgramArguments</key>
            <array>
                    <string>/Library/MyApp/myapp.app/Contents/MacOS/myapp</string>
            </array>
            <key>RunAtLoad</key>
            <true/>
    </dict>
    </plist>
    EOT

The other possibility is a global login item. From that technical note mentioned above, I gleaned:

  • A global login item is a login item that is launched when any user logs in.
  • In Mac OS X 10.5 and later you can install a global login item using the shared file lists interface to Launch Services [API].
  • On earlier systems, you can install a login item [not a global login item] by sending Apple events to the System Events process. [So perhaps this no longer works reliably as of 10.5?]
  • Prior to Mac OS X 10.5 there is no supported way to install a global login item. [Wow! That's crazy.]

I believe that a lot of applications (prior to launchd in 10.5) used to manipulate the loginwindow.plist file directly. Unfortunately, a lot of them did it incorrectly – I can't tell you how many half-XML-half-binary-all-broken loginwindow.plist files I've seen.

share|improve this answer
add comment
up vote 0 down vote accepted

Fission is pretty spot on.

There are some additional details on adding to login items from bash in the following stackoverflow post:

http://stackoverflow.com/questions/6947925/add-app-to-osx-login-items-during-a-package-maker-installer-postflight-script/7643260#7643260

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.