Test if display is asleep / put display to sleep

Hi all.

I need to know how I can test if my display is sleeping and then when my script is finished I just put the display back to sleep again.

I use GUI scripting so that wakes the display up.

I know I can run the app “ScreenSaverEngine” to initiate the screen saver which will eventually sleep the display so I’d like to bypass that too and put the display straight to sleep.

We have an issue with screen brightness here in a room where someone is trying to sleep and a script which runs regularly enough to be a real annoyance!

Thanks

Model: iMac 2006 with OSX 10.4.11
AppleScript: 1.10.7
Browser: Safari 525.13
Operating System: Mac OS X (10.4)

I just worked this out, by comparing the results from the shell command ioreg
Quite proud of my self really if this is right, and it seems to be. I’m sure some one will have a simpler way;)

To detect display sleep.

delay 6 -- test with display awake, then test with display asleep (display sleep hotkeys are ctrl+shift+eject)
set sleeping to 1
set awake to 4
set display_sleep_state to do shell script "ioreg  -n IODisplayWrangler |grep -i  IOPowerManagement"
if display_sleep_state contains sleeping then
	say "display asleep"
else if display_sleep_state contains awake then
	say "display awake"
else
	
	say "unknown"
end if

To force the display to ignore it settings and sleep within 1 minute, this used to work.

set displaysleep to do shell script "pmset force -a displaysleep 1"

The was a command to trick the screen into going to sleep straight away, but I think it got broken in Leopard.

Many thanks Mark.

Part of the problem I have is the learning curve: there are just so many commands and such that make up OSX I just never know where to start and I get tired of reading one shell “man” page after another trying to dig!

The ioreg and pmset commands look like it but will test on my setup. Will post back the result.

(Interesting that the display sleep hotkeys don’t work for me but I suspect I need to set something in Sys Preferences)

Model: iMac 2006 with OSX 10.4.11
AppleScript: 1.10.7
Browser: Safari 525.13
Operating System: Mac OS X (10.4)

I should have said, I tested this on my G4 Powerbook, 10.5xx

I run SecuritySpy 24/7 for IP cameras. When motion is detected it runs this new AppleScript, which would first check if the display is awake, and that could indicate I am on it or motion made the script wake it. I have searched for such a way to check if the display is sleeping and only finding this. It looks perfect but it always says “display asleep” no matter what.
Thanks for all assistance!

on run arg
	set mdCamNumber to item 1 of arg as number -- PLACE CAMERA NUMBER FROM SECURITYSPY INTO VARIABLE
	set mdCamName to item 2 of arg -- PLACE CAMERA NAME FROM SECURITYSPY INTO VARIABLE
	
	set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
	if display_sleep_state contains 1 then
		say "display asleep"
	else if display_sleep_state contains 4 then
		say "display awake"
	end if
end run

I think so: you are going in the wrong direction. The above scripts are only for reading the status of your computer screen and that’s it. It should be awakened by some script that continuously receives snapshots from the camera, compares it to identity, all this is in the background. Having found 2 non-identical snapshots (motion!), your script awakens the screen, shouts “SOS, rob!” etc…

So, you need a script that 1) receives snapshots from the camera. For example, two snapshots in a time, 2) compares them and 3) gives a signal.

Thanks for the reply.You’ve misunderstood me. SecuritySpy is an always-running application that detects motion and runs my .scpt, passing it the camera number. For now I just want to determine if the display is awake. Once that part works I should be good.

Ok, I’ll see how to do it.

Try this:


on run arg
	set mdCamNumber to item 1 of arg as number -- PLACE CAMERA NUMBER FROM SECURITYSPY INTO VARIABLE
	set mdCamName to item 2 of arg -- PLACE CAMERA NAME FROM SECURITYSPY INTO VARIABLE
	
	set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
	if display_sleep_state contains 1 then
		say "display asleep"
	else
		say "display awake"
	end if
end run

NOTE: I believe, this not need at all here (and on run argv may be simple on run):


set mdCamNumber to item 1 of arg as number -- PLACE CAMERA NUMBER FROM SECURITYSPY INTO VARIABLE
set mdCamName to item 2 of arg -- PLACE CAMERA NAME FROM SECURITYSPY INTO VARIABLE

So the entire script is sufficient that:


on run
	set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
	if display_sleep_state contains 1 then
		say "display asleep"
	else
		say "display awake"
	end if
end run

SecuritySpy requires a .scpt file.

I am now testing/working with an idle variable I set with an ioreg echo and if it’s longer than the display sleep setting, then the display is asleep.

I was updated the answer.

I did try that and the result was no different.

Those set lines are to get the number (of 8) and name of the triggering camera.

But why? On my computer this script says “Display awake”, when the display of computer is awake.

It always says awake, that is what does not work.

OK, try this (more essential checking):


on run
	set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
	if display_sleep_state contains "\"CurrentPowerState\"=4" then
		say "display awake"
	else
		say "display asleep"
	end if
end run

And post here, what result gives on your computer this code:


set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"

As the running of script triggers Script Editor to start, this is logical result to be display awake.

The following script will say a sleep state too:


on run argv
	
	set mdCamNumber to item 1 of arg as number -- PLACE CAMERA NUMBER FROM SECURITYSPY INTO VARIABLE
	set mdCamName to item 2 of arg -- PLACE CAMERA NAME FROM SECURITYSPY INTO VARIABLE
	
	repeat
		set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
		if display_sleep_state contains "\"CurrentPowerState\"=4" then
			say "display awake"
		else
			say "display asleep"
		end if
		delay 1
	end repeat

end run

I updated the script to be with arguments. Now, to exit the repeat loop on awake event, add after the code line say “display awake” this:

exit repeat

Then continue process this event as you wish (goes after the end repeat code line)

My mistake, it always said asleep initially, but when I put the numbers in without the variables it switched to always saying awake.

I have put that in place to test on a few cameras though I don’t think I need repeat as SecuritySpy calls the script for each motion event.

OK, here is what I have done. I had to KISS and it does exactly what I want.

on run args
	set mdCamNumber to item 1 of args as number -- PLACE CAMERA NUMBER FROM SECURITYSPY INTO VARIABLE
	set mdCamName to item 2 of args -- PLACE CAMERA NAME FROM SECURITYSPY INTO VARIABLE
	
	set display_sleep_state to do shell script "echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))"
	-- Display Sleep IF
	if display_sleep_state is greater than 5 then
		do shell script "~/SecuritySpy/SleepDisplay -w" -- WAKE DISPLAY ¬
	end if
	
	tell application "SecuritySpy"
		remove full screen camera number -1 screen number 1
		say mdCamName
		add full screen camera number mdCamNumber
		add full screen camera number mdCamNumber screen number 1
		delay 10
		add full screen camera number -1 screen number 1
		delay 10
		remove full screen camera number mdCamNumber
	end tell
end run