Service management (macOS)
Register login items, launch agents, and launch daemons through the macOS SMAppService API from Kotlin.
service-management-macos registers login items, launch agents, and launch daemons on macOS 13+.
The plists are embedded in the bundle at build time, so the services also work in the Mac App
Store sandbox. Add implementation("dev.nucleusframework:nucleus.service-management-macos:3.0.0")
and check AppServiceManager.isAvailable before you register a service.
service-management-macosGradle plugin: RequiredWindow runtime: Not neededNeeds an app bundle and the launch agent plists the plugin generates.Launch the app at login
AppServiceManager.register(AppService.MainApp)
when (AppServiceManager.status(AppService.MainApp)) {
AppServiceStatus.ENABLED -> { /* launches at next login */ }
AppServiceStatus.REQUIRES_APPROVAL -> AppServiceManager.openSystemSettingsLoginItems()
else -> {}
}For launch at login on every OS, use AutoLaunch. Use this module
directly for agents and daemons.
Register a background agent
A launch agent runs in the user session on a schedule, independent of your windows.
-
Declare the agent:
build.gradle.kts nucleus.application { nativeDistributions { macOS { launchAgents { agent("com.example.myapp.sync") { arguments("--sync") startInterval(900) // 15 minutes } } } } } -
Handle the argument in
main(), beforenucleusApplication:fun main(args: Array<String>) { if ("--sync" in args) { performSync() return } nucleusApplication(args) { /* ... */ } } -
Register it from your app with the same label:
AppServiceManager.register(AppService.Agent("com.example.myapp.sync"))
The first registration shows a system notification. The user approves it there, or later in System Settings → General → Login Items.
To fire at wall-clock times, call calendar { } once per moment. Weekdays follow launchd: 0 is
Sunday, 1 is Monday.
agent("com.example.myapp.weekly-cleanup") {
arguments("--cleanup")
calendar { weekday = 1; hour = 18; minute = 0 } // Monday 18:00
calendar { weekday = 5; hour = 18; minute = 0 } // Friday 18:00
}Unlike the scheduler, agents have no constraints or input data, and
the set is fixed at build time. Branch on ExecutableRuntime.isSandboxed() to pick one.
With ProGuard, keep -keep class dev.nucleusframework.servicemanagement.** { *; }.
API reference
AppServiceManager
| Member | Returns | Description |
|---|---|---|
isAvailable | Boolean | true on macOS 13+ with the native library loaded. |
register(service) | Result<Unit> | Registers the service so it can run. |
unregister(service, callback = {}) | Unit | Asynchronous. callback receives an error message, or null on success. |
status(service) | AppServiceStatus | NOT_REGISTERED, ENABLED, REQUIRES_APPROVAL, or NOT_FOUND. |
openSystemSettingsLoginItems() | Boolean | Opens the Login Items pane. |
AppService
| Variant | Use |
|---|---|
MainApp | The app itself as a login item. |
LoginItem(bundleIdentifier) | A helper app in Contents/Library/LoginItems/. |
Agent(label) | A launch agent in Contents/Library/LaunchAgents/. |
Daemon(label) | A launch daemon in Contents/Library/LaunchDaemons/. |
launchAgents { agent(label) { ... } }
| Method | Description |
|---|---|
bundleProgram(path) | Executable inside the bundle. Defaults to the one named after packageName. |
arguments(vararg) | Arguments passed to the program. |
startInterval(seconds) | Fixed interval; 900 (15 minutes) is the recommended minimum. |
calendar { ... } | Wall-clock trigger with month, day, weekday, hour, minute. Repeatable. |
runAtLoad(enabled = true) | Run as soon as launchd loads the agent. |
keepAlive(enabled = true) | Restart the agent when it exits. |
processType(type) | "Background" (default), "Standard", or "Adaptive". |
What's next
- Auto-launch: launch at login on every OS.
- Scheduler: dynamic background tasks with constraints.
- Packaging for macOS: sign and distribute the bundle.
Last updated on