How to automatically launch an application whenever the Mac goes idle

applescript

Is there a way to launch an application, such as Transmission, every time the computer goes idle (instead of sleeping)?

Best Answer

This is a very hard feat to accomplish, primarily because there is no specific definition of "Idle". The only programs I've seen to actually have idle features are Instant Messaging Clients and the recent Mac App Store releases of Growl. And those simply have a configurable time that goes off after the set number of minutes when there's no mouse/keyboard activity occurring.

There's been similar discussion on the Macworld Forums some years ago, and a specific post that essentially entails creating and running an AppleScript that will monitor your usage and launch an application when you go "idle".

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 opens the app. *)
                tell application "TextEdit" to launch   
            end if

            return idleCheck
        end if
    end tell
end idle

In this case idle is only after 2 minutes, but you can easily change that to be longer if necessary. You will also want to modify the line to open Transmission instead of TextEdit.

Note that you will have to open this AppleScript and leave it running in order for the action (launching an application on idle) to ever occur.