 
						The best life is use of willing attitude, a happy-go-lucky life.
— Mr.Wang
 
		  		发布时间:2024-12-14 14:09:12
发布作者:admin
2766
1.Save this code1 as lockWatcher.swift2:
#!/usr/bin/env swift
import Foundation
class ScreenLockObserver {
    init() {
        let dnc = DistributedNotificationCenter.default()
        // listen for screen lock
        let _ = dnc.addObserver(forName: NSNotification.Name("com.apple.screenIsLocked"), object: nil, queue: .main) { _ in
            NSLog("Screen Locked")
            self.runBashScript(path: "~/bin/logout.sh")
        }
        // listen for screen unlock
        let _ = dnc.addObserver(forName: NSNotification.Name("com.apple.screenIsUnlocked"), object: nil, queue: .main) { _ in
            NSLog("Screen Unlocked")
            self.runBashScript(path: "~/bin/login.sh")
        }
        RunLoop.main.run()
    }
    private func runBashScript(path: String) {
        let task = Process()
        task.launchPath = "/bin/bash"
        task.arguments = ["-c", path]
        task.launch()
        task.waitUntilExit()
    }
}
let _ = ScreenLockObserver()
2.Make lockWatcher.swift2 executable:
chmod +x lockWatcher.swift3.Run lockWatcher.swift2 on startup / login: