Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell an Applescript to stop executing

Tags:

applescript

People also ask

How do I stop AppleScript from running?

All replies. Depending on what the script is doing, pressing command-period will cancel it.

What is tell in AppleScript?

Description. The tell compound statement identifies the target of an AppleScript command or Apple event (as in tell app "Photoshop 5.5" ) followed by other AppleScript statements and an end tell .

Does Apple still use AppleScript?

AppleScript is a scripting language created by Apple Inc. that facilitates automated control over scriptable Mac applications. First introduced in System 7, it is currently included in all versions of macOS as part of a package of system automation tools.

How do I run AppleScript automatically?

Click Actions in the top-left corner of the Automator window, then select Utilities in the Library. Drag the Run AppleScript action into your workflow. You can edit, compile, and test your script right in the action, or you can develop your script in Script Editor.


Error number -128 is "User Cancelled", and will stop the script - for example:

if wmColor is false then
    error number -128
end if

"return" tells a script to stop executing:

display dialog "Do you want to exit this script?" with icon note buttons {"Exit", "Continue"}
if the button returned of the result is "Exit" then
  return
end if

For the specific case of an AppleScript invoked via osascript, it can be terminated, returning a status of 0 to the shell, by doing:

tell me to "exit"

Terminate, emitting a message and returning a status of 1, by doing:

tell me to error "{your message}"

The above writes a line like this to standard error:

{scriptname}: execution error: {your message} (-2700)

Here is an example that replaces the cryptic "-2700" and likely does what jefflovejapan is looking for:

tell me to error "Terminated by user" number 0

which writes this to standard error:

{scriptname}: execution error: Terminated by user (0)

and returns a status of 1.

I learned this by experiment, after reading: AppleScript Language Guide - error Statements


I found this works well for scripts running within an application (for example, InDesign CS5.5):

repeat 100 times
    tell application ("System Events") to keystroke "." using command down
    delay (random number from 0.5 to 5)
end repeat

This was modified from this answer.