AppleScript: “tell me to quit” – conditional on the environment

applescriptscript

The "tell me to quit" statement is very useful when you run an applescript as an applet or from the script menu, however when developing or debugging scripts in Script Editor it is very annoying since it quits Script Editor rather than just stopping the execution of the script.

Two questions:

  1. Is it possible from within a script to check what environment it runs in, something like

    if environment is applet then tell me to quit
    else display notification "Done"

  2. How do I exit just the script when it is run in Script Editor? That is, the same result as when you click the stop button in the toolbar.

Best Answer

Current Application

You can get the name of the current application. If the name is Script Editor, then your AppleScript is being run within the editor:

display dialog (name of current application as text)

Detecting the Environment

Alternatively, your AppleScript can access environment variables through the system attribute command. You should be able to determine your process through this information. To see the HOME path environment, use:

display dialog (system attribute "HOME")

Stopping an AppleScript

To stop the script use the return command. In the AppleScript snippet below, the dialog is never displayed:

return
display dialog "Hello World"

return exits the current routine within AppleScript; much like return in other languages such as c. Called within the context of the main AppleScript routine, there is nothing to return to, so the script finishes.

Compare this to tell me to quit, where the snippet causes a quit AppleEvent to be issued to the current process.