How to Get Rid of Apple Script Errors
AppleScript is a language designed to allow Mac OS users to write scripts to interact with the operating system, including manipulating files and sending commands to compatible applications. Sometimes, however, AppleScripts may encounter situations or conditions they weren't written for, and may produce errors that at best interrupt your work, and at worst can corrupt files or crash your system. Fortunately, the AppleScript language provides an easy technique to specify how errors are handled, and exactly how they will interrupt the script's execution, if at all.
Instructions
-
-
1
Enclose statements that may produce errors in a "try" block, and put code to handle errors in a nested "on error" block. Such a structure will generally look like this:
try
[statements]
on error [errorMessage] [errorNumber]
[statements]
end try
By adding variable names as arguments of the "on error" handler (in place of "errorMessage" and "errorNumber"), you can retrieve specific information about any error that occurs.
-
2
If you encounter an error, consult the AppleScript error codes reference (see Resources). You can then write a conditional statement in the "on error" block to respond appropriately to that particular error without interrupting execution of your script.
-
-
3
Consult the AppleScript Lanugage Guide (see Resources) for more detailed error-handling methods if your script requires them. For example, you can retrieve information about which AppleScript object produced the error, and examine results accrued in the routine before the error occurred, that would otherwise be discarded.
-
1