Determine Mac's screen state using AppleScript

5

Is there a way to determine what state the Mac's screen is in? I'd like to have a script that checks if the screen is in a non-active state; so either the screen saver is running or the display is asleep. Can this even be done via applescript?

I'm trying to make a script similar to these proximity detection scripts, except I want them to activate when I've either manually activated the screensaver, put the display to sleep or when the system activates the time based screensaver and display sleep settings.

user47883

Posted 2010-08-29T03:42:49.960

Reputation: 51

Answers

2

Yes, with this code:

set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
if display_sleep_state contains sleeping then
   -- Display is asleep
else if display_sleep_state contains awake then
   -- Display is awake
else
   -- We don't know.
end if

Wuffers

Posted 2010-08-29T03:42:49.960

Reputation: 16 645

Doing all the work using do shell script hardly qualifies as yes ;-) – Daniel Beck – 2011-03-26T16:27:15.047

@DanielBeck: Why? It solves the problem, doesn't it? – Wuffers – 2011-03-26T16:30:10.957

I upvoted because of that -- I referred to this being an AppleScript solution. – Daniel Beck – 2011-03-26T16:42:43.090

The results I get from the first line have no mention of "sleep": "IOPowerManagement" = {"TimeSinceDeviceIdle"=5609187,"DevicePowerState"=4,"CurrentPowerState"=4,"ActivityTickles"=192146,"TimeSinceActivityTickle"=50,"IdleTi$ – Billbad – 2013-02-26T01:03:30.550