I am new to AppleScript and am attempting to use it.

If I am using Terminal and if I have not pressed a key for x minutes I want my Mac to press the return key. It is to repeat this process every y minutes. I would like this process to continue until I close Terminal or stop the AppleScript.

My programming attempt is below:

repeat 52 times
delay 840
tell application "Terminal"
set currentTab to do script ("Return") in front window
end tell
delay 840
end repeat
  • Is it working? What is your question, actually :) – myhd Mar 6 '14 at 18:29

Why not fix it in the terminal?

I would suggest something like

while true;
  do <whatever your terminal actions is>;
  sleep 60; #sleep 60 seconds

This will go in a continuos loop. When the program exits, it jumps to sleep 60. Which will cause it to sleep for 60 seconds and then jump back to the execution part.

Although this is not specifically what you asked for, this might be an easier way to deal with it.

A bit closer to what you are suggesting:

 for i in {1..52};
   do <your action>;
   sleep 840;
  • This doesn't answer the OP's question. – adayzdone Mar 7 '14 at 14:22
  • 3
    Fair point. I merely put this in as an alternative approach. Sometimes having more tools/approaches, gives you new insights on how to address your problem. – Volsk Mar 7 '14 at 14:38
  • Another fair point. – adayzdone Mar 7 '14 at 14:48
  • 1
    This is an answer to the problem a reasonable person would assume exists upon reading the question - no need to delete even totally wrong answers if they move the discussion forward and could be helpful to others with a similar question. – bmike Mar 7 '14 at 19:37
  • @bmike The OP asked to "press the return key". Although Koen van Rhee answer adds a delay, it does not answer the question. – adayzdone Mar 8 '14 at 18:39

Save this as a stay open application:

on idle
    if application "Terminal" is running then
        tell application "Terminal" to set currentTab to do script "" in front window
    end if
    return (14 * minutes)
end idle

What problem are you trying to solve?

For instance, that's a common behavior of an SSH client, and is more easily, and better, solved by adding ServerAliveInterval 60 to your ssh config.


There are two key parts to your AppleScript, pressing a key and performing a task after inactivity.

Pressing a key

Automating a key press in AppleScript is well covered; the accepted answer to the linked question suggests:

tell application "System Events" to key code 36

Performing when inactive

The second part, performing a task when your Mac has been idle is more tricky. You can use third party software to schedule your AppleScript after inactivity, or you can use a scripted approach to extract the idle time information from the system:

ioreg -c IOHIDSystem | perl -ane 'if(/Idle/) {$idle=(pop @F)/1000000000; print $idle, "\n"; last;}'

Wrapping this up in AppleScript becomes:

set inactive_seconds to do shell script "ioreg -c IOHIDSystem | perl -ane 'if(/Idle/) {$idle=(pop @F)/1000000000; print $idle, \"
\"; last;}'"

Where inactive_seconds contains the number of inactive seconds.

Putting it together

Putting it together depends on your looping approach. Consider a while loop that checks if the Terminal.app is running. John Gruber's article will be helpful for this, How to Determine if a Certain App Is Running Using AppleScript and Perl:

tell application "System Events"
    count (every process whose name is "Terminal")
end tell

The greater while loop could be:

tell application "System Events"
    repeat while ((count (every process whose name is "Terminal")) > 0)

        -- check idle time and conditionally perform key press
        -- wait n minutes to avoid excessive polling

    end repeat
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .