Created
February 1, 2018 16:51
-
-
Save fbatroni/24ce336cb1c03f172fffb101e65104bc to your computer and use it in GitHub Desktop.
AppleScript - Launch command if computer has been idle for X seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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