How to do simple error catching in Applescript.

Like a noob, I posted this in the OS X area as opposed to here. Aaaanyway, here it is again…

Hi. I’ve been in the process of learning how to error trap everything trappable in a script I’ve written to let it run unattended and thought I’d share what I found. If anyone knows ways to do it better or wants to extend it, please fell free to school me on better ways to do it.

The code below simply stored whether an error has happened or not. 1 is a fail, 0 is success. The basic observation is that you have to wrap every operation you need to trap for success of failure in a try block.

Cheers,

  • Alex Zavatone
 on run
    -- start of error checking block
    try
        set errorOccurred to false
        activate application "TextEdit"
        
    on error errMsg number errNum
        set errorOccurred to true
    end try
    
    log errorOccurred
    -- end of error logging block
    
    -- start of error checking block
    try
        set errorOccurred to false
        quit application "TextEdit"
    on error errMsg number errNum
        set errorOccurred to true
    end try
    log errorOccurred
    -- end of error logging block
end run

  • Zav