MacOS – AppleScript: Press Enter Every X minutes If Terminal is Open and Application Has Been Idle for X Minutes

applescriptmacosterminal

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

Best Answer

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
done;

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;
 done