telling if macOS screen is locked from bash script
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 | |
set -euo pipefail | |
# from https://stackoverflow.com/a/66723000 | |
function screenIsLocked { [ "$(/usr/libexec/PlistBuddy -c "print :IOConsoleUsers:0:CGSSessionScreenIsLocked" /dev/stdin 2>/dev/null <<< "$(ioreg -n Root -d1 -a)")" = "true" ] && return 0 || return 1; } | |
function screenIsUnlocked { [ "$(/usr/libexec/PlistBuddy -c "print :IOConsoleUsers:0:CGSSessionScreenIsLocked" /dev/stdin 2>/dev/null <<< "$(ioreg -n Root -d1 -a)")" != "true" ] && return 0 || return 1; } | |
if screenIsLocked; then | |
echo "Screen locked" | |
fi | |
if screenIsUnlocked; then | |
echo "Screen unlocked" | |
fi | |
if ! screenIsLocked; then | |
echo "Screen unlocked (inverse logic)" | |
fi | |
if ! screenIsUnlocked; then | |
echo "Screen locked (inverse logic)" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment