I have these 4 triggers and would like another one “At lock”:
but the lock-trigger is missing in KM. Claude code suggested to use this swift code and installed it with launchd and it worked immediately as intended and coexists now with the KM macros:
import Foundation
let logFile = "/Users/gg/logs/lock-unlock.log"
func appendLog(_ event: String) {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let line = "\(formatter.string(from: Date())) \(event)\n"
guard let data = line.data(using: .utf8) else { return }
if FileManager.default.fileExists(atPath: logFile),
let handle = FileHandle(forWritingAtPath: logFile) {
handle.seekToEndOfFile()
handle.write(data)
handle.closeFile()
} else {
try? data.write(to: URL(fileURLWithPath: logFile))
}
}
let center = DistributedNotificationCenter.default()
center.addObserver(forName: NSNotification.Name("com.apple.screenIsLocked"),
object: nil, queue: .main) { _ in
appendLog("LOCK")
}
RunLoop.main.run()
I’ll dump it, as soon as KM has a lock trigger. Output:
2026-04-23 13:47:16 IDLE
2026-04-23 13:51:06 LOCK
2026-04-23 13:56:17 SLEEP
2026-04-23 14:17:04 WAKE
2026-04-23 14:17:08 UNLOCK
