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 (
ServiceManagementframework) or command-line tool (launchctl).Using API
SMJobBlesscreates a daemon—process, running under root. If you wish to create an agent—process, running under another user, use theSMLoginItemSetEnabledfunction. There is a slight difference between these functions:SMJobBlessregisters an executable andSMLoginItemSetEnabledaccepts bundle id as main parameter, so for an agent you need to create a Cocoa app.SMLoginItemSetEnabledis much easier to set up.SMJobBlesscopies the executable toLibrary/PrivilegedHelperToolsand launches it from there, not from your bundle.SMLoginItemSetEnabledlaunches the agent from your bundle, without copying it.SMJobBlessrequires the user to enter admin password;SMLoginItemSetEnableddoesn't bother the user with anything, it does not require root rights.Using launchctl
SMLoginItemSetEnabledregisteres 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.plistfile into/Library/LaunchAgents(you'll need root rights to do that). In order to load it immediately uselaunchctltool (from code—viaNSTask). Documentation on configuration files:man launchd.plist.