A shell tool for Mac login items.

Manipulating login items (you know, the apps that launch when you log into your Mac) from code is tricky. Apple suggests a number of ways to do it; the least painful seems to be to use AppleScript to do it (and that’s certainly not to say that it’s not plenty painful, too). The recent Cuckoo bug fix revolved around some AppleScript that works fine for most users, but blows up inscrutably in rare cases (in particular under 10.5 “Leopard”).

While testing the new release I cooked up loginitem, a command-line tool that can be used to quickly add, remove, query, and list all the login items for the currently logged-in user. It’s a Python script that uses the Objective-C bridge and AppleScript to bark orders at System Events to get the job done. I found it to be safer than screwing around with defaults and more convenient than fishing through System Preferences. You could conceivably employ this script as part of a software installer, or use it while isolating a login item that’s giving you trouble (a technique suggested here). It does quite a bit, so there are probably other uses I haven’t thought of as well.

Here’s an example session with loginitem:

Set Terminal to launch on login:

$ loginitem --add /Applications/Utilities/Terminal.app

Did it work?:

$ loginitem --exists Terminal \
    && echo "yup, it worked"
yup, it worked

Let’s see it another way:

$ loginitem --list
[...]
Terminal
[...]

$ loginitem --list-paths
[...]
/Applications/Utilities/Terminal.app
[...]

On second thought, no, don’t bother:

$ loginitem --remove Terminal

Hey, what happens if we try that again?

$ loginitem --remove Terminal
error: login item "Terminal" does not exist

Note that the results returned from --list are UTF-8 encoded localized names, so if you’ve got an app whose name in the Finder has non-ASCII characters (Quicksilver is egregious in this respect), just be careful.

The loginitem script is freely available under a BSD license. Please get in touch if you find bugs or have suggestions.


About this entry