Is it possible to close a script with a keypress? In AutoHotKey, using the keyword &quot;ExitApp&quot; can allow the user to stop a script with a keypress.  For example, Esc::ExitApp can stop a script by pressing &quot;esc&quot;.
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 &quot;stay open&quot; - 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 &lt; 100
       keystroke &quot;a&quot;
       set i to i + 1
       delay 5
     end repeat
end run
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 &quot;System Events&quot;
    set pidList to unix id of application processes whose name is &quot;osascript&quot;
end tell

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

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.