Tell Blocks Considered Harmful (was Re: open for access)
Tell Blocks Considered Harmful (was Re: open for access)
- Subject: Tell Blocks Considered Harmful (was Re: open for access)
- From: Chris Page <email@hidden>
- Date: Tue, 02 Dec 2008 20:51:03 -0800
On Dec 2, 2008, at 5:14 AM, Nigel Garvey wrote:
An access to a file belongs to the application that opened it. In a
script, that'll be either the application running the script or, if
the
'open for access' command's in a 'tell application ...' block, the
target
of the 'tell'. Only that application can close (or use) the access, so
everything to do with that access has to be done under the same 'tell'
conditions.
Please indulge me while I issue the standard rant about tell blocks,
since they've been on my mind a lot lately:
Although judicious use of tell blocks to address specific objects can
make code clearer when you're performing several consecutive
operations on the same object, most of the time they're unnecessary,
and they can cause problems when inside "tell application" blocks.
Unnecessarily placing commands within tell blocks makes the code
harder to understand, puts it at risk for terminology collisions and
makes the script susceptible to breaking if applications or scripting
additions change (e.g., if an application adds an implementation of a
command that it didn't have before, it will now override the command
your script was originally intended to use.) It also reduces
performance to unnecessarily send events to other processes, which is
slower than handling them within the current application.
You should favor individual "tell ... to" statements for events that
must be sent to other applications and use "using terms from" when all
you need is the terminology without altering the current target
("it"). Only code that actually must be within a tell should be placed
within tell blocks (and "using terms from").
Note that when an application returns an object specifier as a result,
you can use that specifier outside a tell block or statement for
subsequent commands. Commands sent to that specifier will be sent to
the appropriate application. You then only need "using terms from" if
the commands or other parameters require that terminology. e.g.,
tell application "Finder" to get item 1
get the name of the result
produces the events and replies (this is a verbose description; this
isn't what Script Editor places in the log)
tell application "Finder" to get item 1
--> file "foo" of application "Finder"
tell application "Finder" to get name of file "foo"
--> "foo"
Since the term "name" isn't specific to Finder, you can get the name
of a Finder object without a "tell" or "using terms from". In fact,
"item" isn't specific to Finder, either, so you could just write
get item 1 of application "Finder"
and skip the "tell" altogether.
As for "open for access" and the other I/O commands, specifically, it
is a very bad idea to send them to other applications:
1. It forces an arbitrary program to open a file on your behalf and to
maintain an open file handle within its process state until you close
it. Mucking with the state of other programs should be limited to
debuggers. Only an application program that explicitly implements such
a feature should let you do that. It is an accident of history that
scripting additions enable arbitrary programs to respond to events
they were not explicitly designed to support.
2. If the target application process has different privileges than the
sender, the script may break because the target doesn't have
permission to access the same files as the sender. Even if the script
works today, it may not in the future.
Keep tell blocks to a minimum and don't send scripting addition
commands to other programs unless absolutely necessary, especially
ones that alter process state.
Note: Even something as seemingly innocuous as "random number" alters
process state. The seed for the "random number" command is a global
variable stored within each process. If an application sets the seed
to a specific value in order to produce a particular pseudo-random
number sequence and another application tells it to generate random
numbers, they could interfere with one another's sequences. You should
at the very least use "my (random number)" or "random number of me"
within tell blocks to be safe.
--
Chris Page
The other, other AppleScript Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden