AppleScript Tutorial: Error Handling (try... on error)

21 February 2009

Many people have been asking about Error Handling in AppleScript so we decided to put together a quick tutorial on the subject.

We are going to split the error handling tutorial into two sections

  • Simple error handling
  • Advanced error handling


Simple Error Handling

AppleScript will try to execute a section of code and if it fails then it can be redirected to execute another code. Because AppleScript is English based the syntax need is “Try” used as follows.
Before we try to capture an error, let’s create some code that creates and error.

The following code attempts to open a file for reading but due to the filename path being in the wrong format, an error occurs.

For more information about filename paths, see the File and Folder Path Tutorial

Example AppleScript Code:

-- create a filename path

set FileName to "/Users/computer/Desktop/filename.txt"


-- Open will not be able to open a filename because the path is not in the correct format.

open for access FileName


This creates the following error:

The issue is that when this error comes up, the whole AppleScript stops at this point. In cases when we are repeating actions on multiple file, we want to skip some errors so that we can continue with the rest of the script.

Now, to simply capture this error and display an error notification without stoping the rest of AppleScript from being executed, we use the “Try” code as follows:

Example AppleScript Code:

try

   -- create a filename path

   set FileName to "/Users/computer/Desktop/filename.txt"


   -- Open will not be able to open a filename because the path is not in the correct format.

   open for access FileName


on error

   display dialog "There was an Error" with icon 0

end try


The result is that the AppleScript tries to execute the previous code and when it detects an error, the code in the “on error” section is executed.

The result is as follows:

For more information on display dialogs, please refer to the Dialog Tutorial

Simple Error Handling can be very useful when first developing your code. Once you have the code doing what you want, adding some Advanced Error Handling is a good idea.


Advanced Error Handling

When you wish to know exactly what went wrong, including some Advanced Error Handling is a good idea.

Using the above code, we are going to add in some Advanced Error Handling.
We look for a specific error and if found, we fix the error before trying to execute the “open” command again.

Example AppleScript Code:

try

   -- create a filename path

   set FileName to "/Users/computer/Desktop/filename.txt"


   -- Open will not be able to open a filename because the path is not in the correct format.

   open for access FileName


on error errText number errNum

   log {errText, errNum}

 

   if (errNum = -1409) then

      try
         -- Convert the filename to the correct format
         set FileName to POSIX file FileName

         -- try to open the file again
         open for access FileName

      on error errText number errNum
         log {errText, errNum}

         --Capture the error
         display dialog errText

      end try
   end if
end try


The result is as follows:

And that covers it. To download the complete AppleScript, click download AppleScript below. Download Example AppleScript



Reference Documents

To review the complete AppleScript Guide as well as other AppleScript Guides for MS Word, Excel, PowerPoint or Adobe Photoshop please review the “Reference Documents” page on this website.
AppleScript Reference Documents

Best of luck with your AppleScripting adventures.

Regards,

Thompson-Solutions