A way to check for sleep state of display using KM?

Hi, I upgraded to an M1 Macbook pro — deep thanks to Peter for making the M1 version so quickly — and one of my scripts that checks the state of the computer's display then does something if it's awake (e.g. if I'm using that computer currently) no longer works. The old code was

tell application "Terminal"
set display_sleep_state to do shell script "echo $(ioreg -n IODisplayWrangler | grep -i IOPowerManagement | perl -pe 's/^.DevicePowerState"=([0-9]+).$/\1/')/4 | bc" as string
end tell

but this doesn't function under M1 that I can see, only Intel.

So I was wondering if there is a decent way to see if the screen is sleeping currently using KM or Terminal? Mainly I need to run reminders for myself and don't want them going off on my work iMac and my sometimes-in-use M1 Macbook.

Quoting function:SCREENSAVER [Keyboard Maestro Wiki]

The SCREENSAVER() function returns whether the Mac is currently displaying the screen saver (or the display is off). It returns 1 if the screen saver is running, or the display is sleeping or locked. Generally, if it returns 1 it is probably not safe to perform UI.

Here’s an example of how you can use it in an IF / ELSE in Keyboard Maestro:

You could, for example, use an IF / ELSE and put a ‘Cancel Macro’ in the first block if you do not want the rest of the macro to run while the screensaver is on.

1 Like

Thank you very much! As usual, Keyboard Maestro makes everything on the Mac better. I have issues with my Apple Watching telling me to stand, but tapping too silently, so I have actual notifications to stand when the watch wants me to. These show on all my devices, even the ones that are asleep at home. This will save me lots of hassle!

1 Like