property idleTime : 0
delay 3
set idleTime to (do shell script “ioreg -c IOHIDSystem | perl -ane ‘if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,"";last}’”) as number
always gives the expected result (3.036578 with slight variations) when run in the Script Editor
The same holds for:
property idleTime : 0
delay 3
set idleTime to (do shell script “ioreg -c IOHIDSystem | perl -ane ‘if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,"";last}’”) as number
tell application “System Events”
display dialog idleTime giving up after 3
end tell
So far so good. However, the script:
property idleTime : 0
delay 3
tell application “System Events”
set idleTime to (do shell script “ioreg -c IOHIDSystem | perl -ane ‘if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,"";last}’”) as number
display dialog idleTime giving up after 3
end tell
sometimes gives the right value but most of the time (yeah, I know how this sounds) it gives 0 as a result. Furthermore all variants described above give 0 as a result when the script is run with Cron /usr/bin/osascript /Users/me/Library/Scripts/crons/timer.scpt. Scripts without the delay and/or without the “property idleTime : 0” are identical. Now, there seems to be some consistency in these inconsistencies because all variants, even the Cron-triggered scripts may give the correct results the first couple of times the script is run but once the result is 0 it remains so. Restarts do not solve the problem. Does anyone has a clue how to solve this?
strange phenomenon - it seems to be an applescript prob. I tried this: sleep 3 ; ioreg -c IOHIDSystem | awk ‘/HIDIdleTime/ {print $NF/1000000000; exit}’
runs ok every time I try it in the shell - but as soon as I run it in a ‘do shell script’ it fails.
Can’t you solve your prob as pure shellscript?
If you need to run the ‘delay 3’ as applescript you may try this (from the shell): osascript -e “delay 3” ; ioreg -c IOHIDSystem | awk ‘/HIDIdleTime/ {print $NF/1000000000; exit}’
works ok from the command line but the results are a little bit higher.
Certainly seems to be an AS problem. I now have an app writing idle time to a file which is read by the script. Not very elegant but it works. While doing that I found out why some of the code yielded inconsistent results (sometimes the idle time, sometimes 0). The application “myIdle”:
on idle {}
set idleTime to (do shell script “ioreg -c IOHIDSystem | perl -ane ‘if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,"";last}’”)
set idleTime to idleTime as string
do shell script "defaults write " & myMempath & " myIdle " & quoted form of idleTime
return 5
end idle
writes 0 to the file myMempath unless the application “myIdle” has been opened by the Finder. In the cron timed script that needs obviously a running application “myIdle”, the code ‘tell application ‘‘myIdle’’ to run’ gives you an application writing 0 to the file while the code 'tell application “Finder” to open application file path_to_myIdle" gives you an application writing idle time to the file