Snow Leopard and sleep ...


Having problems with the sleep function (programmed to sleep after 15 minutes of inactivity) on my my new iMac Intel (it never went to sleep even if I unplugged everything … USB and USB hub, ethernet, etc …) I wrote a little handler to «handle» this situation.

I thought that everything was fixed, but now it goes to sleep faster than the handler is setup to. Even if it is setup to sleep afetr 900 seconds, it does after 90.

Here is the handler:

property idleTime : 0
property theTextToLog : ""

on idle
	set idleTime to (do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'")
	if idleTime is greater than 900 then
		say "Sleep will be set in 20 seconds ...... "
		do shell script "/bin/sleep 15"
		if idleTime is greater than 900 then
			set idleTime to 0
			do shell script "/bin/sleep 10"
		end if
	end if
end idle

on PowerManagerSleep(vDelay)
	tell application "Power Manager Scripting"
		tell scheduler
			add event {enabled:true, unique id:"Mise en veille", name:"Mise en veille", trigger:{type:once, date:((current date) + vDelay)}, action:{type:sleep}}
		end tell
	end tell
end PowerManagerSleep

You need Power Manager to run this handler. My version of the handler includes a «log» function that helps me see what is going on. This is how I can see when it really goes to sleep.

Is Snow Leopard scripting modified from the Tiger version, requiring other statements that I don’t know of ?

Thanks in advance.


Don’t laugh … I changed the delay to 9000 and to (15 * minutes) and the handler puts my computer to sleep after ±90 seconds.

It took me a while to find a work around, but I found one. My problem was with this line:

set idleTime to (do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'")

By adding the “round” function the handler now work perfectly.

set idleTime to round(do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'")

I don’t know the reason the script didn’t work without the “round” function. Maybe some of you would be nice to explain the problem I experienced.

The full silence I got with this problem on the forum, makes me feel that the solution was obvious, but for me it is not. And I worked hard finding the or a solution.

So if some of you understand this case, I would be glad to learn