2

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
2

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
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .