Discussion:
How do I bend launchd to my will?
Gregory Weston
2011-06-30 22:30:44 UTC
Permalink
In a new app I'm writing I want to provide the user with a checkbox to have the app launch at login. Traditionally, I'd add an entry to the login items list for this, but I'm under the impression that the modern, enlightened coder uses a well-formed plist in ~/Library/LaunchAgents. This seemed like a good idea, given that I'd like to pass it arguments when it's launched that way as well.

From reading various documents, I have the notion in my head that I'm supposed to *just* be able to put that plist out there. For example, Apple says (at http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Articles/LaunchOnDemandDaemons.html):

| When a user logs in, a per-user launchd is started. It does the following:
| • It loads the parameters for each launch-on-demand user agent from the property list files found in
| /System/Library/LaunchAgents, /Library/LaunchAgents, and the user’s individual Library/LaunchAgents
| directory.

And Big Nerd Ranch's "Advanced Mac OS X Programming" says (on page 477):

| There are two mechanisms for getting your configuration plist onto launchd's list of dameons.
| The first is putting your plist into one of a number of pre-defined directories, and the other
| is via the launchctl command-line utility.

But as you probably know, or have guessed since I'm writing, this appears not to be the case. My plist is not recognized on login, or even after a reboot. If I invoke launchctl it works fine, but doing that from code seems bizarrely tedious for something that a lot of software seems to do. Even if I do that I'm faced with the annoying behavior that if I use launchctl unload from within my app (when the user unchecks the box) it will cause the app to exit. Am I going about this the wrong way? Am I missing something that should be obvious? Should I go back to a login item entry?

Thanks in advance for any pointers.
Kyle Sluder
2011-06-30 22:34:10 UTC
Permalink
Post by Gregory Weston
In a new app I'm writing I want to provide the user with a checkbox to have the app launch at login. Traditionally, I'd add an entry to the login items list for this, but I'm under the impression that the modern, enlightened coder uses a well-formed plist in ~/Library/LaunchAgents. This seemed like a good idea, given that I'd like to pass it arguments when it's launched that way as well.
If you're really talking about launching your app at login, you still
want to use the Login Items list via the LSSharedFileList API.
http://cocoatutorial.grapewave.com/tag/lssharedfilelist-h/

Login Agents are for background helper apps that need to connect to
the window server (usually to communicate with other apps). Login
Daemons are for background services that don't need to connect to the
window server.

--Kyle Sluder
Gregory Weston
2011-07-01 12:04:36 UTC
Permalink
Post by Kyle Sluder
Post by Gregory Weston
In a new app I'm writing I want to provide the user with a checkbox to have the app launch at login. Traditionally, I'd add an entry to the login items list for this, but I'm under the impression that the modern, enlightened coder uses a well-formed plist in ~/Library/LaunchAgents. This seemed like a good idea, given that I'd like to pass it arguments when it's launched that way as well.
If you're really talking about launching your app at login, you still
want to use the Login Items list via the LSSharedFileList API.
http://cocoatutorial.grapewave.com/tag/lssharedfilelist-h/
Login Agents are for background helper apps that need to connect to
the window server (usually to communicate with other apps). Login
Daemons are for background services that don't need to connect to the
window server.
I'm okay with the notion that this isn't really the right technique for this particular app. Thanks. I'm still curious about the underlying question though. I had actually originally split this in two parts, where there *was* a lightweight agent and a foreground controller/config tool. So what does it take to get an agent plist recognized? Are the implications of Apple's page and the explicit claim in the Big Nerd Ranch book wrong, or outdated, or was I just missing something?
Loading...