On 11/30/07 3:02 PM, Mark Lively <[log in to unmask]> wrote:

> On Nov 30, 2007, at 2:12 PM, Stockly, Ed wrote:
> 
>> Is there a shell command that will return how much time has passed
>> since the
>> last time a user moved or clicked the mouse or hit a key on the
>> keyboard?
>> 
>> ES
> 
> A little Google-fu led me to this charm.
> 
> ioreg -n IOPower -l |grep -i idle
> 
> It gives idles for various devices.  They seem to be in order of last
> use but that might not always be the case.  Time is expressed in 10^-9
> seconds!  They claim it works in panther.  I know it works in leopard.

The following syntax works in both Panther (10.3) and Tiger (10.4) but has
not be tested in Leopard (10.5):

----- Attachment follows -----

get "ioreg -c IOHIDSystem | awk '/Idle/ {x = $5} END {print x/1000000000}'"

(do shell script result) as number
-- seconds computer has been idle

----- End of attachment -----


You can even do something similar in Jaguar (10.2), but the output must be
converted to decimal time.  In case anyone is interested, here's a version
of code I used back then:

----- Attachment follows -----

get "ioreg -c IOHIDSystem | " & ¬
    "awk '/Idle/ {x = $5} END {print x}' | tr -d '[:punct:]'"

set IdleTime to do shell script result
-- get system idle

set IdleTime to do shell script "perl -e 'print 0x" & IdleTime & "'"
-- convert idle time to decimal (before 10.3)

return (IdleTime as number) / 1.0E+9
-- seconds computer has been idle

----- End of attachment -----


I have found getting the idle time useful for setting up Stay-Open
AppleScript applets that wait until there is no user interaction before
executing.

-Jeffrey Berman