MacOS – How to determine if the screen is asleep in Mountain Lion, from the command line

command linemacosscreensleep-wake

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…

Best Answer

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))