Last active
August 18, 2024 21:37
-
-
Save stephen-fox/ec873f9012e203e6a947320a0aa21f6a to your computer and use it in GitHub Desktop.
macos: Check if screen is locked from shell script
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/sh | |
# screen_is_locked is based on work by Joel Bruner: | |
# https://stackoverflow.com/a/66723000 | |
# | |
# For an Objective-C implementation, see: | |
# https://stackoverflow.com/a/76241560 | |
# | |
# This version avoids creating temporary files (via '<<<') by using plutil. | |
# | |
# Returns 0 if screen is locked. | |
screen_is_locked() { | |
[ "$(/usr/sbin/ioreg -n Root -d1 -a \ | |
| /usr/bin/plutil \ | |
-extract 'IOConsoleUsers.0.CGSSessionScreenIsLocked' \ | |
raw -)" \ | |
== 'true' ] | |
return $? | |
} | |
# Example: | |
if screen_is_locked | |
then | |
echo "screen is locked" | |
else | |
echo "screen is -not- locked" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment