Files
luxe-macos-tray-notifier/Sources/LuxeNotifier/LoginItem.swift

22 lines
770 B
Swift
Raw Normal View History

import Foundation
import ServiceManagement
/// SMAppService only works from an installed .app bundle, so report that plainly instead of failing.
enum LoginItem {
static var isAvailable: Bool { Bundle.main.bundleIdentifier != nil }
static var isEnabled: Bool { isAvailable && SMAppService.mainApp.status == .enabled }
static func set(_ enabled: Bool) {
guard isAvailable else {
NSLog("LuxeNotifier: launch at login needs the installed .app bundle (run `make install`)")
return
}
do {
if enabled { try SMAppService.mainApp.register() } else { try SMAppService.mainApp.unregister() }
} catch {
NSLog("LuxeNotifier: could not change launch at login: \(error)")
}
}
}