In AutoHotKey, using the keyword "ExitApp" can allow the user to stop a script with a keypress. For example, Esc::ExitApp can stop a script by pressing "esc".

Now, with a combination of do shell script (using a bash function like read) and on idle and then calling exit somehow, I thought this was possible in AppleScript. However, inside of an Automator function, you can't save an AppleScript as "stay open" - pasting the on idle thing underneath on run only loops through idle once without providing opportunities for listening for keypresses. Any ideas?

Example: Let's say I have this script in an automator workflow:

on run {input, parameters}
    set i to 0
     repeat while i < 100
       keystroke "a"
       set i to i + 1
       delay 5
     end repeat
end run
  • Why are you using Automator to run AppleScript, are there other actions occurring before/after that you need it to perform in conjunction with the AppleScript? May 7, 2021 at 20:57
  • This may need to be closed. Can you edit in the post a script which would be eligible to be stopped? Interrupt handling and main loops are more of a swift and Objective-C idiom and scripts run to completion generally. Passing signals through named pipes sometimes works for pure shell scripts, but I don’t get why shell and AppleScript are chosen for this problem tbh.
    – bmike
    May 7, 2021 at 23:32

1 Answer 1


I’m not quite sure if my solution will help you or not.

I have a whole bunch of different AppleScripts attached to folders by use of Folder Actions and I also run AppleScripts directly from the Script Menu in the menu bar.

Occasionally one of my AppleScripts decides to misbehave and freezes or gets stuck and trying to quit the running AppleScript from the spinning gear in the menu bar does not work either.

My solution in these circumstances is to run this following AppleScript code.

tell application "System Events"
    set pidList to unix id of application processes whose name is "osascript"
end tell

set text item delimiters to space
do shell script ({"kill -9", pidList} as text)
set text item delimiters to ""

I saved the AppleScript code as a .scpt file and run it using Voice Commands, but you can just as easily use the code in a new Automator Quick Action then assign it a keyboard shortcut in System Preferences.

NOTE: One of the drawbacks of this solution is that if you are running several scripts at the same time, this will force quit each one. As far as I know, there is no way of knowing which osascript belongs to which running AppleScript. (Not knowing which is which forces me to force quit each one.)

Again, I don’t know if any of this will help you or not. If nothing else maybe it will help spark some ideas for you.

  • Hm... when I log the pidList using script editor, there are no results. It's quite odd - any ideas what could be wrong?
    – Aaron
    May 7, 2021 at 22:18
  • Furthermore, I tried doing ps -ax | grep "osascript" and for some odd reason the output looked the same whether or not an automator quick action was running. imgur.com/a/z9bjohd
    – Aaron
    May 7, 2021 at 22:25
  • @Aaron For the code to work, what is running needs to be a .scpt file. Not a script saved as an application or an Automator workflow or running a script through Script Editor. That was the reason I included the second sentence at the top of my post.
    – wch1zpink
    May 7, 2021 at 22:42
  • Ack! I'll see if I can turn all my workflows into .scpt just to make everything a bit easier. Well, keystrokes aren't working - I guess I'm stuck to closing them by hand ;-;
    – Aaron
    May 7, 2021 at 22:52

You must log in to answer this question.

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