global quit_after, check_every, quitTime
set quitTime to 0
set quit_after to 60
set check_every to 5
set minute to quit_after / 60
display dialog "Start " & check_every & " sec. Starstidan kommer att laddas om efter " & minute & " minuter av inaktivitet."
on reopen
display dialog "reopen Kontroll sker var " & check_every & " sekund. Starstidan kommer att laddas om efter " & minute & " minuter av inaktivitet."
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))"
set idletime to idletime as integer
if quitTime is 1 and (idletime < quit_after) then
set quitTime to 0
end if
if quitTime is 0 and (idletime > quit_after) then
set quitTime to 1
tell application "Safari"
activate
try
repeat
close tab 1 of window 1
end repeat
end try
tell application "Safari"
open location "http://www.google.se"
end tell
end tell
end if
return check_every
end idle