Try
A Try statement is a compound statement consisting of a list of AppleScript statements followed by an error handler to be executed if any of the statements cause an error message.SYNTAX
try [ statement ]... on error � [ errorMessageVariable ] � [ number errorNumberVariable ] � [ from offendingObjectVariable ] � [ partial result resultListVariable ] � [ to expectedTypeVariable ] [ global variable [, variable ]...] [ local variable [, variable ]...] [ statement ]... end [ error | try ]wherestatement is any AppleScript statement.
errorMessageVariable (an identifier) is a parameter variable for the expression, usually a string, that describes the error. You use this parameter variable to refer to the error expression within the error handler.
errorNumberVariable (an identifier) is a parameter variable for the error number (an integer). You use this parameter variable to refer to the error number within the error handler.
offendingObjectVariable (an identifier) is a parameter variable for the reference
to the application or object that caused the error (a reference). You use this parameter variable to refer to the object that caused the error within the
error handler.resultListVariable (an identifier) is a parameter variable for the results for objects that were handled before the error occurred. Its value is a list that can contain values of any class. You use this parameter variable to refer to the partial results within the error handler. This parameter applies only to commands that return results for multiple objects. For example, if an application handles the command
get words 1 thru 5
and an error occurs when handling word 4, thepartial result
parameter contains the results for the first three words.expectedTypeVariable (an identifier) is a parameter variable for the expected value class (a class identifier)--that is, the value class to which AppleScript
was attempting to coerce the value of offendingObjectVariable. If an application receives data of the wrong class and cannot coerce it to the correct class, the value of this parameter variable is the class of the coercion that failed. (The example at the end of this definition demonstrates how this works.)variable is an identifier for either a global variable or a local variable that can be used in the handler. The scope of a local variable is the handler. You cannot refer to a local variable outside the handler. The scope of a global variable can extend to any other part of the script, including other handlers and script objects. For detailed information about the scope of local and global variables, see "Scope of Script Variables and Properties," which begins on page 252.
EXAMPLES
The following Try statement provides an error handler for the Choose File command. (For a complete description of the Choose File command, see the AppleScript Scripting Additions Guide.) The Choose File command returns an error if the user clicks the Cancel button in the Choose File dialog box. The error handler gives the user a chance to continue if an error occurs.
try choose file set fileName to result on error errText number errNum display dialog "An error has occurred: " & ÿ errText & "\rDo you want to continue " & ÿ "using the default file?" ÿ buttons {"Cancel", "Continue"} default button 1 if button returned of result = "Cancel" error number -128 --quit silently else display dialog "The script will continue " & ÿ "using the default file." set fileName to defaultFileName end if end tryFor the preceding example to work correctly,defaultFileName
must have been set to a filename earlier in the same script.The next example demonstrates the use of the To keyword to capture additional information about an error that occurs during a coercion failure.
tell application "Scriptable Text Editor" try repeat with i from 1 to "Toronto" i end repeat on error from obj to newClass {obj, newClass} end try end tell --result: {"Toronto", integer}The Repeat statement fails because the string "Toronto" is the wrong class. The error handler simply returns the values ofobj
(the offending value,"Toronto"
) andnewClass
(the class of the coercion that failed,integer
) in the result window.