Re: 'aete' & AppleScript suite question
Re: 'aete' & AppleScript suite question
- Subject: Re: 'aete' & AppleScript suite question
- From: Shane Stanley <email@hidden>
- Date: Fri, 05 Apr 2002 11:32:04 +1000
On 5/4/02 10:48 AM +1000, has, email@hidden, wrote:
>
Very informative; and an interesting glimpse
>
into a little bit of AppleScript history.
While I'm at it, this was brought up here a while back, and is covered in
the same document:
Easier Error Handling
In AppleScript 1.1 we streamlined the way you can catch and handle
individual errors. Often times it is necessary for a script to handle a
particular error, but not others. In AppleScript 1.0 you had to make sure
that the errors you didn't want to handle were "reraised" with another call
to error. For example:
try
open for access file "Sequentialism:PhoneNumbers" with write permission
on error msg number n from f to t partial result p
if n = -49 then
display dialog "I'm sorry but the file is already opened."
else
error msg number n from f to t partial result p
end if
end try
This script trys to open a file with write permission, but if it is already
opened it displays a dialog instead. In AppleScript 1.1 you can now write
this more concisely as:
try
open for access file "Sequentialism:PhoneNumbers" with write permission
on error number -49
display dialog "I'm sorry but the file is already opened."
end try
Note that we didn't need to mention anything about the message, from, to, or
partial result parameters in order to pass them along. If the error was not
-49 this error handler will not catch the error, and error handlers in an
outer scope will be tried.
You can catch multiple errors by nesting this sort of try statement --
either lexically or within the dynamic scope of the computation. (Remember
that lexical scope means "textually contained in another block structure,"
and dynamic scope means "in the call chain, i.e. from subroutine to
subroutine.") Here's an example of lexical nesting:
try
try
open for access file "Sequentialism:PhoneNumbers" with write
permission
on error number -49
display dialog "I'm sorry but the file is already opened."
end try
on error number -35
display dialog "I'm sorry but the folder is missing."
end try
Here's an example of nested error handlers in the dynamic scope (i.e. errors
are caught even if they are raised in subroutine calls):
on OpenPhoneNumbers()
try
open for access file "Sequentialism:PhoneNumbers" with write
permission
on error number -49
display dialog "I'm sorry but the file is already opened."
end try
end OpenPhoneNumbers
try
OpenPhoneNumbers()
on error number -35
display dialog "I'm sorry but the folder is missing."
end try
One drawback with this scheme is that the error numbers must be literal
constants in the error handler parameter list. You can't define global
variable or property names for these because they must be known when the
script is compiled.
How can you know the values of these important error numbers? One way is
to look them up in a Macintosh reference that describes what errors can
result from specific operations (script error numbers are the same as
regular system error numbers). Perhaps a better way is to simply try the
operation that may raise the error in such a way that actually does raise
the error, and catch and return the error number with a generic error
handler:
try
open for access file "Sequentialism:PhoneNumbers" with write permission
on error number n
n
end try
--> -49
From there you can then replace n in the error handler parameter list with
the -49 directly, and replace n in the body of the error handler with the
appropriate error handling operations.
--
Shane Stanley, email@hidden
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.