Skip to content

Instantly share code, notes, and snippets.

@fbatroni
Created February 1, 2018 16:51
Show Gist options
  • Save fbatroni/24ce336cb1c03f172fffb101e65104bc to your computer and use it in GitHub Desktop.
Save fbatroni/24ce336cb1c03f172fffb101e65104bc to your computer and use it in GitHub Desktop.
AppleScript - Launch command if computer has been idle for X seconds
property idleTest : 180 -- How long to be idle (in seconds) before activating script
on idle_time()
set idleTime to (do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'")
return idleTime
end idle_time
script should_run_matrix
set idleComp to false
-- this repeat loop polls the idle time every 30 seconds to determine
-- whether the system has been idle for the number of seconds defined above.
-- It will repeat indefinitely until the idle time is acheived
-- whereupon the action is performed
repeat while idleComp is false
set idleTime to idle_time() of parent
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set idleTime to ((first text item of idleTime as string) as integer)
set AppleScript's text item delimiters to old_delims
set idleTest to (idleTest as integer)
if idleTime is greater than idleTest then
set idleComp to true
try
if application "iTerm" is running then
tell application "iTerm"
activate
tell current session of current window
write text "unimatrix"
end tell
end tell
else
activate application "iTerm"
end if
end try
else
delay 30
end if
end repeat
end script
tell application "System Events"
tell should_run_matrix to run
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment