The documentations states, that with SMJobBless you can use only kSMDomainSystemLaunchd
SMJobBless(kSMDomainSystemLaunchd, (CFStringRef)label, authRef, (CFErrorRef *)error);
But how to create a LaunchAgent inside LaunchAgents/ in users library, that executes on user login using SMJobBless or similar approach?
This is an old question, but maybe someone has the same issue.
There are 2 main approaches to add an agent: using system API (
ServiceManagement
framework) or command-line tool (launchctl
).Using API
SMJobBless
creates a daemon—process, running under root. If you wish to create an agent—process, running under another user, use theSMLoginItemSetEnabled
function. There is a slight difference between these functions:SMJobBless
registers an executable andSMLoginItemSetEnabled
accepts bundle id as main parameter, so for an agent you need to create a Cocoa app.SMLoginItemSetEnabled
is much easier to set up.SMJobBless
copies the executable toLibrary/PrivilegedHelperTools
and launches it from there, not from your bundle.SMLoginItemSetEnabled
launches the agent from your bundle, without copying it.SMJobBless
requires the user to enter admin password;SMLoginItemSetEnabled
doesn't bother the user with anything, it does not require root rights.Using launchctl
SMLoginItemSetEnabled
registeres the agent only for the current user. If you need to add an agent, which will be launched automatically for any user, that logges in, put a configuration.plist
file into/Library/LaunchAgents
(you'll need root rights to do that). In order to load it immediately uselaunchctl
tool (from code—viaNSTask
). Documentation on configuration files:man launchd.plist
.