Hello,
My script is written to run a loop 30 times only sometimes I need to stop it first.
Is there a way or a letter on the keyboard that makes an emergency STOP?
Thank you
Hello,
My script is written to run a loop 30 times only sometimes I need to stop it first.
Is there a way or a letter on the keyboard that makes an emergency STOP?
command-. will trip an applescript error
The AppleScript equivalent is error number -128.
sorry but I didnāt understand your answer.
How can I create an error with applescript?
Here is my CDB 10 script
On my custom on-screen keyboard, I want to create a key that gives me an error from my Apple script to stop for example the script when I press STOP
Hi @sg72.
I think you need to be clearer about what it is you need. estockly has correctly answered your question about whether or not thereās a letter on the keyboard that makes an emergency stop. But of course the Command-. keystroke can only stop something thatās currently happening in the application receiving it. Itās equivalent to clicking a āCancelā button in the application. Itāll only stop a script if the applicationās running the script.
The same āCancelā code can be generated within a script to stop it by using the AppleScript command error number -128. In this case, the script needs to be written so that this line is executed if a condition is recognised where the script must stop.
At the moment, all we know is that you have a script which issues a series of keystrokes, presumably to be received by the unknown application shown in the screenshots above.
Sorry if I wasnāt clear enough. I will try to clarify my problem. So here is the script that I run with Apple script. Indeed, this allows me to create a series of letters that is executed 30 times.
on run {input, parameters}
delay 1.0
tell application "System Events"
repeat 30 times
keystroke "A"
delay 0.4
keystroke "R"
delay 0.4
keystroke "B"
delay 0.4
key code 53
delay 0.4
keystroke "B"
delay 0.4
key code 53
delay 0.4
end repeat
end tell
end run
Only sometimes I need to stop this script, and so I wish I could have an emergency stop system.
Here is the condition
If CBG30.app is running and STOP is pressed, this stops CBG30.app
So I want to put this emergency stop on my on-screen keyboard with a button. How to do ?
OK. If Iāve understood correctly, the still unidentified application in the screenshots displays buttons in its window which have been set up to run scripts that have been saved as applications.
Assuming the application itself isnāt scriptable, and not thinking about where the keystrokes are going, you could try writing the keystroke scripts in the following way, saving them as stay-open applications. (Thereās a checkbox for this in the āSaveā¦ā dialog.) Instead of quitting after executing its run
handler, the script applet stays open and is periodically told by the operating system to execute its idle
hander. (An idle
handler should return a number indicating the number of seconds to wait before itās called again.) The script below quits after its idle
handler has been executed 30 times or if it receives a quit
command from somewhere else in the meantime.
global counter
on run {input, parameters}
set counter to 0
delay 1.0
end run
on idle
set counter to counter + 1
tell application "System Events"
keystroke "A"
delay 0.4
keystroke "R"
delay 0.4
keystroke "B"
delay 0.4
key code 53
delay 0.4
keystroke "B"
delay 0.4
key code 53
end tell
if (counter = 30) then
quit
else
return 0.4 -- Next idle after 0.4 seconds.
end if
end idle
The āSTOPā button script should be saved as an ordinary application (not stay-open) and should be something along these lines:
on run {input, parameters}
-- This list should contain the names of the other button apps.
-- The apps must exist even if they're not actually running.
set buttonAppNames to {"CBG10", "CBG30", "CBG50", "CBG100"}
repeat with appName in buttonAppNames
tell application appName
if (running) then
quit
exit repeat
end if
end tell
end repeat
end run
The above idea works in principle, but I donāt know enough about what youāre doing to guarantee that itāll work for your situation.
Nigel, our new friend is trying to set up a custom keyboard in Keyboard Viewer.
Gear menu (top right) > Customizeā¦
[ I dug around the web a bit to find this]
Thanks @alastor933.
Using your information (the gearās an ellipsis on my systems), I was able to set up a couple of buttons in the editor with the above two script apps attached. But nothing happened when I clicked the buttons, so maybe they need to be enabled somewhere else. Hopefully sg72 will have this under control.
At this point itās only a guess, but it appears that the OP is running his AppleScript as an AppleScript Application. If thatās correct, he could terminate the AppleScript Application as shown below. I tested the following with an AppleScript Application named āSay OKā. The OP would have to change this to the name of his AppleScript Application.
use framework "AppKit"
use framework "Foundation"
use scripting additions
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set theApps to theWorkspace's runningApplications()
set thePredicate to current application's NSPredicate's predicateWithFormat:"(localizedName == 'Say OK')"
try
set theApp to (theApps's filteredArrayUsingPredicate:thePredicate)'s objectAtIndex:0
on error
display dialog "Your script was not running" buttons {"OK"} cancel button 1 default button 1
end try
theApp's forceTerminate()
Finally it works I created a STOP.APP file in which I put
on run {input, parameters}
-- This list should contain the names of the other button apps.
-- The apps must exist even if they're not actually running.
set buttonAppNames to {"CBG10", "CBG30", "CBG50", "CBG100"}
repeat with appName in buttonAppNames
tell application appName
if (running) then
quit
exit repeat
end if
end tell
end repeat
end run
sg72. Iām glad you found a solution.
BTW, assuming CBG10 is an AppleScript application that sends keystrokes, Iām surprised this solution works, as Iāve had no success using the quit command to stop a running AppleScript application. Perhaps CBG10 is a regular app of some sort, in which case this would make sense.
How do I know or show you?
A running AppleScript application does observe any quit command it receives, but only when itās finished what its currently doing ā ie. running the script, which is when it would normally quit anyway. Thatās why I suggested the idle
handler approach. If a stay-open script is simply hanging around waiting for the next idle
call and notices itās received a quit command, it should quit more or less then. However, from the screenshot in post #13, it looks as if the scriptās being saved as an Automator application, not as an AppleScript one, which is probably why the parameter list was included originally. I donāt know if this introduces any complications.
Thanks Nigelāthat makes perfect sense but made me wonder why the OPās approach works. Youāre probably right that itās the Automator involvement.
Just out of curiosity, I created a shortcut app with my Say OK script, and I was able to do a regular quit of that app while it was running. Itās probably the same with Automator.
sg72. You have a solution that works and thatās great. No reason to consider anything else (not that you would).
Just for the sake of completeness:
Thanks.