Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm new to programming and AppleScript and I have lots of problems with error handling.

Could anybody share their error handling for file operations like move/trash files? Here is my code so far:

try
tell application "Finder" to open file file_path
on error e number n
if e contains "Can’t get file" then say "mount external drive" --file not found, maybe this hard drive is not mounted
if e is x then something
end try
share|improve this question
add comment

1 Answer 1

In general it's advisable to handle errors based on the error number and not on the text (which is language dependent and much more difficult to handle than some numbers). In addition start with just displaying the error number/messages to understand what really went wrong:

try
tell application "Finder" to open file file_path

on error error_message number error_number
display dialog "Error: " & the error_number & ": " & the error_message buttons {"OK"} default button 1
end try

Once you know the potential error codes you want to handle specifically you can extend this to

try
tell application "Finder" to open file file_path

on error error_message number error_number
    if error_number = -1728 then
        display dialog "Can't read file."
    else
        display dialog "Error: " & the error_number & ": " & the error_message buttons {"OK"} default button 1
    end if
end try
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.