How long has your machine been idle?

I’ve often wondered how to know this. Here are two ways (in a script that gives a delay so you’ll see if you don’t touch anything after you click “Execute”.


delay 8
-- From http://www.macosxhints.com/article.php?story=20040330161158532
set iT_1 to (do shell script "echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))") as real
-- From Mark Hunte
set iT_2 to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'") as real

Who cares? Suppose you want to do some maintenance routine if the machine has been idle for a while. An on idle stay-open application will do the maintenance on every return, but this will wait:


property howLong : 600 -- ten minutes
on idle
	set idleTime to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'") as number
	if idleTime > howLong then
		-- Post a notice and do your maintenance
	end if
	return howLong
end idle

hi adam,

i found these while researching the same thing.

here is a link to where i found them. easy enough to make them ‘do shell scripts’ if needed.

http://gridengine.sunsource.net/servlets/ReadMsg?list=users&msgId=22785

[code]Here is the SED version (all one line)

echo $((ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/!{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q' / 1000000000))

Here is the perl version:

ioreg -c IOHIDSystem | perl -ane ‘if(/Idle/) {$idle=(pop @F)/1000000000;
print $idle, “\n”; last;}’

Here is the AWK version:

ioreg -c IOHIDSystem | awk ‘/HIDIdleTime/ {print $NF/1000000000; exit}’[/code]
cheers.

Whoa! How can I get this to keep running in the background so it runs my script after I’m away from the computer for 10 minutes?