Re: Weird try block syntax
Re: Weird try block syntax
- Subject: Re: Weird try block syntax
- From: Bill Cheeseman <email@hidden>
- Date: Sat, 26 Jan 2002 07:03:50 -0500
on 02-01-25 2:59 PM, Christopher Nebel at email@hidden wrote:
>
On Friday, January 25, 2002, at 04:30 AM, Serge Belleudy-d'Espinose
>
wrote:
>
>
> try
>
>
>
> on error number -n
>
>
>
> end
>
>
>
> with n being just about any number needed.
>
>
>
> I didn't know such construct existed. My first reaction would be that
>
> this is bogus, but who am I to contradict Jon Pugh? :)
>
> So is it a feature of Jon's commands- which I'm using actually- or
>
> from an editor I'm not using (only Smile and SE?)
>
>
It's a long-standing but little-known feature of AppleScript; nothing to
>
do with Smile or Jon's Commands. I didn't see any references to it in a
>
quick scan of the ASLG, but maybe it appears in an example somewhere.
OK, OK. I dug out my trusty old AppleScript 1.1 SDK CD, and launched the
Scriptable Text Editor in the Classic environment of Mac OS X, and reread
the AppleScript 1.1 release notes and related materials....
Isn't it amazing that Scriptable Text Editor runs in Mac OS X? And the
information in these old release notes is amazing -- available nowhere else.
The discussion of the differences between linked lists and vector lists
alone justifies re-reading the 1.1 release notes from time to time.
By the way, I also have the hard-copy documentation for the AppleScript 1.0
SDK. I wonder where I put the disks....
Back to our regularly scheduled topic:
From the AppleScript 1.1 SDK "General Notes" file:
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.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont