Here is an old script I had,that you can play with.
It will check the idle state every 20 seconds.
if the idle state is less than 20 seconds ( i.e a key clicked in the last 20 seconds) then it will quit in this case textedit.app.
if the idle time is more than 120 seconds ( no key clicked for a minute and half) it will launch textedit.app
Code:
property idleCheck : 20 as integer
property idleCheck_usr : 120 as integer
set timer to 0
on idle
--Check idle time
set idletime to do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'"
set idletime to idletime as string
set idletime to idletime as integer
tell application "System Events"
if idletime is less than idleCheck then (* 20 is 20 seconds. If a key was tapped
within the idleCheck seconds, it quits the app. *)
tell application "TextEdit" to quit
return idleCheck -- checks again in ... seconds
else
if idletime is greater than idleCheck_usr then (* If a key was tapped
after the idleCheck_usr seconds it t opens the app. *)
tell application "TextEdit" to launch
end if
return idleCheck --
end if
end tell
end idle