Last active
March 30, 2025 09:13
-
-
Save mieky/d57f3dd99f137fb62cd68d91d6478359 to your computer and use it in GitHub Desktop.
Zero-dependency watcher for macOS session lock/unlock events
This file contains hidden or 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 | |
# | |
# Watches for macOS session lock/unlock events and runs ~/.lock or ~/.unlock on change. | |
# | |
# Usage: | |
# - chmod u+x /path/to/lockwatch.bash | |
# - to test, run /path/to/lockwatch.bash | |
# - to launch in the background, set up a launchd agent with lockwatch.plist. | |
script_lock=~/.lock | |
script_unlock=~/.unlock | |
unlocked=-1 | |
unlocked_last=-1 | |
# detect if a copy of lockwatch is already running | |
if pgrep -f lockwatch.sh >/dev/null; then | |
echo "lockwatch is already running" | |
exit 1 | |
fi | |
echo "lockwatch started" | |
while true; do | |
ioreg -n Root -d1 -a | grep -q CGSSessionScreenIsLocked | |
unlocked=$? | |
# Update status after a change | |
if [[ $unlocked -ne $unlocked_last && $unlocked_last -ne -1 ]]; then | |
# shellcheck disable=SC1090 | |
if [[ $unlocked -eq 0 ]]; then | |
echo "$(date +'%c') - screen is locked" | |
# if the script specified by path script_lock exists, run it | |
[[ -f $script_lock ]] && source $script_lock | |
else | |
echo "$(date +'%c') - screen is unlocked" | |
[[ -f $script_unlock ]] && source $script_unlock | |
fi | |
fi | |
unlocked_last=$unlocked | |
sleep 1 | |
done |
This file contains hidden or 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//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<!-- | |
- Save this file in ~/Library/LaunchAgents/lockwatch.plist | |
- Update the path to the script in the ProgramArguments array | |
- Start the script with: launchctl load ~/Library/LaunchAgents/lockwatch.plist | |
- To later stop it, use: launchctl unload ~/Library/LaunchAgents/lockwatch.plist | |
--> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>lockwatch</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/Users/mike/bin/lockwatch.bash</string> | |
</array> | |
<key>RunAtLoad</key> | |
<true/> | |
<!-- | |
OnDemand makes it easier to stop the script when needed, without it will always auto-restart. | |
--> | |
<key>OnDemand</key> | |
<true/> | |
</dict> | |
</plist> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
~/.lock
: