Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

share|improve this question
    
I too have been trying to find a way to query the display sleep state from the command line. Coming up empty so far. :( –  ipmcc Sep 26 '13 at 12:25

2 Answers 2

up vote 1 down vote accepted

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))
share|improve this answer
    
Hmmm... this might be a good alternative to sensing the Screen Saver or Display Sleep status. Thanks. Still would like the original answer as well, but this might be a good alternative. –  G Piper Dec 10 '12 at 4:23
    
Wait... your command gives me this error: "Illegal variable name" –  G Piper Dec 10 '12 at 4:25
    
Did you copy/paste the command? Mixing up the different kinds of quotes and backticks will break it. Just in case copying doesn't work here use this paste bin pastebin.com/7JrPNxYU –  MacLemon Dec 10 '12 at 12:47
    
Yes, I cut and pasted it. Tried the cut-and-paste bin too... no luck. Same error. I've been cutting and pasting for 18 years too. (-; It doesn't seem to work on 10.8.2? I can see what you're trying to do with it, too... I may try to roll my own from that, but having difficulty. –  G Piper Dec 10 '12 at 16:35
    
It works perfectly on my machines running 10.6.8 (bash) as well as 10.8.2 (zsh). (I don't have a Lion Mac at hand.) You could also try to derive from echo $(( $(ioreg -c IOHIDSystem | grep HIDIdleTime | sed -e 's/[^0-9]//g') / 1000000000 )) –  MacLemon Dec 10 '12 at 16:57

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.