loading SMAppService LoginWindow agent

I have an app that I would like to register a launchd agent at the login window.

my plist looks like this:

<?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>RunAtLoad</key>
	<true/>
	<key>LimitLoadToSessionType</key>
	<array>
		<string>LoginWindow</string>
	</array>
	<key>AssociatedBundleIdentifiers</key>
	<string>com.myapp</string>
	<key>Label</key>
	<string>com.myapp.login-window</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/local/myapp.app/Contents/MacOS/myapp-agent</string>
		<string>--login-window-agent</string>
	</array>
</dict>
</plist>

If I manually copy the com.myapp.loginwindow.plist to /Library/LaunchAgents and load it manually it works as I'd expect

If I use SMAppService though it does not work:

let service = SMAppService.agent(plistName: "com.myapp.login-window.plist")

do {
    try service.register()
} catch let error {
    print(error.localizedDescription)
}  

I get:

The operation couldn’t be completed. Service cannot load in requested session.

That makes sense as I'm not at the login window when I run that routine.

However checking service.status() (with formatting) I get

LaunchAgent(com.myapp.login-window.plist) status: Enabled

which would suggest that it's loaded, yet when I log out or reboot and I'm at the login window the agent is not running. Indeed the agent does not appear in launchctl list

I'm not sure what else to check to see what's going on.

I've tested loading LaunchAgents and LaunchDaemons in other contexts using the ServiceManagement framework and these all work as I'd expect once approved. It's just the LoginWindow agent that is having issues.

following https://developer.apple.com/documentation/servicemanagement/updating_helper_executables_from_earlier_versions_of_macos and updated the plists to use the BundleProgram key but I still can't get it to operate.

in fact now regular agents and daemons don't load. launchctl list shows a LastExitStatus of 19968.

Trying to do this the apple way for Ventura and beyond but I'm not finding it intuitive. Need more coffee or sleep (or both)

Accepted Answer

I have an app that I would like to register a launchd agent at the login window.

I suspect that the SMAppService API wasn’t designed to do this. I encourage you to file a bug describing your requirements and your experiences and requesting direct support for your case. Please post your bug number, just for the record.

If you’d like me to dig into this in more depth you should feel free to also open DTS tech support incident but, honestly, I doubt that I’ll be able to find a workaround for this (well, other than to not use SMAppService and continue with the code you’re currently using).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

loading SMAppService LoginWindow agent
 
 
Q