How can I determine if the screen is asleep in Mountain Lion, from the command line?
I have a script that needs to run every so often, but only if the screensaver is active or if the screen is asleep (to make sure nobody is actively using the computer first). I need to have a way to determine whether the screen is asleep from the command line. (One-liner preferred)
I'm using "ps -acxw | grep ScreenSaverEngine | grep -v grep" to determine if the screensaver is running, which always worked fine in previous OS versions, but in Mountain Lion, the screensaver is disabled when the screen goes to sleep, so I need a way to determine that as well now.
Help is appreciated...
Though not directly answering your question this line will give you the time in seconds since the last user interaction which comes from what OS X considers to be an idle user. (independently of screen-savers or black displays.)
echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))
I think I've figured it out. This does it for me:
ioreg -n IODisplayWrangler | grep -i IOPowerManagement | perl -pe 's/^.*DevicePowerState\"=([0-9]+).*$/\1/'
As for the number that this returns, in my case it appears to be in the range of 0-4. I don't know what each number specifically means for sure, but when the display is "on", I get 4, and when the display has been put to sleep by idle I get 0. I assume the in-between states are for dimming, etc, but I don't have a laptop here, so I'm not sure.