STOP FORCING applescript

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

command-. will trip an applescript error

Ok but they are a point ?

or

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]

1 Like

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.

So I created an AppleScript application like below, but when I run the script nothing happens

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

Look here

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:

  • is CBG10 a regular application like Safari or Mail; or
  • is CBG10 an AppleScript that you saved in Script Editor as an application; or
  • is CBG10 an AppleScript that you saved in Automator as an application; or
  • is CBG10 something else?

Thanks.