2

For years, I've used Applescripts to check if my Mac's monitor is locked (not to be confused with checking if the screen saver is active. I don't have the screen saver lock the screen since I use the screen saver as a giant clock when I'm home and I lock the screen when I leave).

Either the switch to Apple silicon or a Mac OS update has stopped scripts like this from checking if the screen is locked:

set isScreenLocked to do shell script "pmset -g ps | awk 'NR==2{print $2}'"

Scripts like this don't seem to work on Apple silicon either:

set isScreenLocked to do shell script "ioreg -n IODisplayWrangler | grep -i IOPowerManagement"

I'm running Mac OS Sonoma on an M1 Mac studio. How can I use Applescript to check whether or not the screen is locked (again, not to be confused with the screen saver being active)?

2
  • What do you expect the output of pmset -g ps to be, when locked? The man page says that displays the battery status. (Also, I'm curious: what does the script do when the screen is locked?)
    – benwiggy
    Commented Jan 23 at 17:17
  • The script is for home automation stuff. When the monitor isn't locked, it locks it. When the monitor is locked, it tells other smart devices in my home to not do certain things.
    – 2oh1
    Commented Jan 23 at 20:23

1 Answer 1

-3
[ "$(/usr/libexec/PlistBuddy -c "print :IOConsoleUsers:0:CGSSessionScreenIsLocked" /dev/stdin 2>/dev/null <<< "$(ioreg -n Root -d1 -a)")" = "true" ] && echo 0 || echo 1;

Works on my macOS 15.

Found it here: https://www.reddit.com/r/applescript/comments/det3go/comment/inqowmi/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

3
  • 1
    Can you please explain what this does and how it can be used in AppleScript as the OP asked?
    – nohillside
    Commented Oct 25 at 6:26
  • 1 means activated, and 0 means locked. You can test with: sleep 5; xxx, enter and lock the screen
    – Kros-ZERO
    Commented Oct 31 at 12:24
  • Might help to explain that in your answer, and also how the script works.
    – nohillside
    Commented Oct 31 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .