- sudo chmod +x /opt/screenLW.sh
- sudo chmod +x /usr/local/bin/tg_say
- launchctl load /Library/LaunchAgents/screen_locker.plist
- launchctl start /Library/LaunchAgents/screen_locker.plist
Created
February 24, 2025 12:56
-
-
Save santaklouse/f27cf19c1b85e58b50ef0fb4059406c2 to your computer and use it in GitHub Desktop.
macbook lock/unlock notifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>screen-locker-logger</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/opt/screenLW.sh</string> | |
</array> | |
<key>RunAtLoad</key> | |
<true/> | |
<key>KeepAlive</key> | |
<true/> | |
</dict> | |
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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") | |
self.runNotifierScript(status: "MacBook locked!") | |
} | |
// 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") | |
self.runNotifierScript(status: "MacBook Unlocked!") | |
} | |
RunLoop.main.run() | |
} | |
private func runBashScript(path: String) { | |
let task = Process() | |
task.launchPath = "/bin/bash" | |
task.arguments = ["-c", path] | |
task.launch() | |
task.waitUntilExit() | |
} | |
private func runNotifierScript(status: String) { | |
let task = Process() | |
task.launchPath = "/usr/local/bin/tg_say" | |
task.arguments = [status] | |
task.launch() | |
task.waitUntilExit() | |
} | |
} | |
let _ = ScreenLockObserver() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -interpret /opt/screenLockWatcher.swift -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -color-diagnostics -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name screenLockWatcher -disable-clang-spi -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
API_TOKEN='<BOT_API_TOKEN>' | |
CHAT_ID='<CHAT_ID>' | |
API_URL="https://api.telegram.org/bot$API_TOKEN" | |
if [ -z "$CHAT_ID" ]; then | |
wget -qO "${API_URL}/getUpdates" | |
exit 1 | |
fi | |
MSG="<b>$(hostname)</b>: $@" | |
if [[ -z $@ ]]; then | |
while read data; do | |
MSG=$MSG''$data | |
curl -sS -X POST "${API_URL}/sendMessage" -d "chat_id=${CHAT_ID}" -d parse_mode=html -d "text=${MSG}" >/dev/null 2>&1 | |
MSG='' | |
done | |
exit 0 | |
fi; | |
curl -sS -X POST "${API_URL}/sendMessage" -d "chat_id=${CHAT_ID}" -d parse_mode=html -d "text=${MSG}" >/dev/null 2>&1 | |
if [[ $? -eq 0 ]]; then | |
exit 0 | |
else | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment