Custom Error Message

Hi All

I am new to AppleScript, first attempt to create one. I am using it for automating a task with BBEdit.

The scripts asks for an input from the user, if the input is ok the script runs fine.

I cannot test the input ( a string) until BBEdit process it, so if the string is not correct I get a nasty error message from BBedit.

Is it possible to suppress this error message and create a custom one with AppleScript?

I have tried with "try" statment but even then I first get the error message from bbedit and then my custom one.

Any help/answer is appreciated.

macmini, macpro, macbookpro, Mac OS X (10.5.6)

Posted on Jan 28, 2009 3:17 AM

Reply
6 replies

Jan 28, 2009 9:34 AM in response to gIOve

It might have helped if you'd shown your code - that way we could highlight specific issues, but in general the format you're looking for is:

tell application "BBEdit"
try
-- your code here
on error errMsg number errNum
-- an error occurred, so tell the user
display dialog "Oops. Something went wrong. The error message was '" & errMsg & "' (" & errNum & ")"
end try
end tell


This could produce an error message like:

Oops. Something went wrong. The error message was 'File not found' (-43)


Technically the 'errMsg' and 'errNum' parameters on the 'on error' line are optional - they're only needed if you want to capture the actual error message and/or number returned by the application that the the error. For example, if you know what specific error numbers mean you could customize your action based on the errNum (some might not need reporting to the user, others might cause you to take additional action in your script, etc.).

Jan 29, 2009 9:56 AM in response to gIOve

Ahh, interesting - it appears that BBEdit's 'open location' command throws an error on its own before reporting back to the calling AppleScript.

This is something that should be passed back to Bare Bones since they are the only ones that can suppress that initial warning (maybe they can detect the fact the open was issued via AppleScript and adjust their actions accordingly).

Jan 28, 2009 12:54 PM in response to Camelot

Another option is to use a custom error message; very handy when using subroutines with try statements inside an application.

tell application "BBEdit"
try
-- your code here
on error errMsg number errNum
-- tell the user using an error dialog instead of standard dialog
error "Oops. Something went wrong. The error message was '" & errMsg & "' (" & errNum & ")"
end try
end tell

Get's a little tricky depending on how the target application handles AppleScript errors (for example, when running the script with Script Editor the error dialog happens in Script Editor, not BBEdit).

Jan 28, 2009 1:37 PM in response to Pandrake

You're correct, using the error command would allow you to post a more informative error message to the user. However 'error' would still terminate the script, and I interpreted the OP's request as more wanting to intercept the error and take corrective action, which was why I just used 'display dialog' as an example.

I suppose without knowing the original intent it's hard to know which works best.

Jan 29, 2009 6:15 AM in response to Pandrake

Hi Camelot, Pandrake

thanks for your posts. You have got the code right, but I still get 2 error messages one from BBedit and another one from AppleScript. Here's my code

tell application "BBEdit"
try
open location( ftp://etc etc) with passive tranfer
on error errMsg number errNum
display dialog "Some Error Message '" & errMsg & "' (" & errNum & ")"
end try
end tell

If the FTP location is right BBEdit opens the file, if the location is wrong I get the error meggage "this operation could not be completed etc etc." and then my custom one -

I would like to suppress the first one but maybe is not possible.

cheers

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Custom Error Message

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.