Skip to content

Instantly share code, notes, and snippets.

@stephen-fox
Last active August 18, 2024 21:37
Show Gist options
  • Save stephen-fox/ec873f9012e203e6a947320a0aa21f6a to your computer and use it in GitHub Desktop.
Save stephen-fox/ec873f9012e203e6a947320a0aa21f6a to your computer and use it in GitHub Desktop.
macos: Check if screen is locked from shell script
#!/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