Skip to content

Instantly share code, notes, and snippets.

@anatomatic
Last active April 8, 2020 00:14
Show Gist options
  • Save anatomatic/5736900 to your computer and use it in GitHub Desktop.
Save anatomatic/5736900 to your computer and use it in GitHub Desktop.
Applescript to quit nvALT when idle
-- adapted from: https://culturedcode.com/forums/read.php?7,30174,30597
(*
save as 'stay open' Application and launch at startup
to run in background, change info.plist file to include
<key>LSBackgroundOnly</key>
<true/>
*)
global quit_after, check_every
set quit_after to 1200 -- seconds
set check_every to 30 -- seconds
-- keep dialogs below commented if info.plist has LSBackgroundOnly set to <true/>
(* display dialog "System Idle check is performed every " & check_every & " seconds. nvALT will be quit after " & quit_after / 60 & " minutes of system inactivity." *)
(* on reopen
display dialog "System Idle check is performed every " & check_every & " seconds. nvALT will be quit after " & quit_after / 60 & " minutes of system inactivity."
end reopen *)
on idle
set idletime to do shell script "echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))"
if (idletime as integer) > quit_after then
tell application "System Events"
if ((name of processes) contains "nvALT") then
tell application "nvALT" to quit
end if
end tell
end if
return check_every
end idle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment