9.3. The error Command

You can use the AppleScript error command to explicitly generate an error yourself. You can use a built-in error message or one of your own creation. You can also assign error numbers in your programs.

The most general form of the error command is as follows:

error eMessage number eNumber partial result eList from eFrom to eTo

Here the error message string stored in eMessage is displayed to the user. The error number is indicated by eNumber. The remaining parameters are rarely used by programmers, but are typically passed along to AppleScript for further error processing.

Unless the error command is executed inside a try block, the program's execution is terminated after the error command is executed and the dialog is acknowledged.

All the error command's parameters are optional. If you just write the following:

error

AppleScript puts up a dialog saying that "An error has occurred." If you specify a message, as in

error "I couldn't open your database file!"

then the specified message is displayed to the user. If you omit the message, but just supply a number, as in

error 1001

a dialog is displayed with just the error number (which is not very helpful). Apple has reserved all integer numbers for possible error codes except the numbers 1,000 through 9,999 inclusive. This means that you can assign you own error codes for errors that occur in your program. This is often done inside a handler to indicate to the caller of the handler that an error occurred. If ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.