Re: Handlers and Tell Blocks
Re: Handlers and Tell Blocks
- Subject: Re: Handlers and Tell Blocks
- From: has <email@hidden>
- Date: Wed, 28 Aug 2002 22:40:38 +0100
Paul Berkowitz, in demonstrating the patience of a true saint, wrote:
>
If
>
you call a handler within a tell block, the tell block has no effect on the
>
handler.
Ahh, I get you this time. As contrasted against [e.g.] considering/ignoring
blocks, which have an effect on all code executed within the block,
including code in other handlers called from within said block.
>
The handler needs its own internal tell blocks,
...to communicate with the desired target, yes. As if breaking out of a
tell block by making a local handler call loses sight of the target object
you'd previously locked on to.
One option if you're communicating heavily with one particular object, and
don't want to use explicit tell blocks all the time, is to take that object
as the parent property for your script [object]:
property parent application : "Microsoft Entourage"
[Though I wouldn't really recommend folk use this unless they've already
got a good idea of how inheritance and scope work, otherwise it's real easy
to tie yourself in knots.]
I think a secondary problem with writing clean, simple code arises from the
terminology-supporting function of tell blocks: think how much simpler it'd
be if you could just write:
set myFile to file "foo" of folder "bar" of ... application "Finder"
at one point in your script, and thereafter pass that value around on the
rest of the script by itself:
get name of myFile
tell myFile to move to outFolder
Then you wouldn't have to worry about sticking explicit application tell
blocks all over the place just to keep the mechanics of AS rolling
smoothly. [Well okay, you _can_ do this sort of thing this if you really,
really want to. However, you then have to mess around with the clumsy
'using terms from' construct to keep the compiler right, which kinda
defeats the purpose. Plus everyone else starts getting jittery when they
read your straaange-looking code; heck, you'll probably feel pretty darn
jittery yourself. In short, not worth the hassle.:p]
>
The whole discussion, which I know was very confusing, was prompted by a
>
study of the Mail Scripts which Apple kindly provided. [...] Like this:
>
>
>
tell application "Microsoft Entourage"
>
tell eachContact
>
theScript's setFirstName(newEntry, first name)
>
theScript's setLastName(newEntry, last name)
>
theScript's setTitle(newEntry, title)
I find this just a little too clever for its own legibility, what with the
way the tell blocks and object references have all been nested. I would
tend to lay it out as follows - while it's a bit longer, it's also very
straightforward to read:
tell application "Microsoft Entourage"
tell eachContact
set firstName to first name
set lastName to last name
set theTitle to title
end tell
end tell
--
tell theScript
setFirstName(newEntry, firstName)
setLastName(newEntry, lastName)
setTitle(newEntry, theTitle)
end tell
Now, whether there's any practical difference other than improved
readability, such as the first version being faster, I don't know (and even
if it was a bit faster, you really have to consider the difference in terms
of the overall running time before deciding if it's a. significant, and b.
worth compromising your code's clarity for). But it is very easy to get
carried away in the first flush of OOD and deep-nest your tell blocks at
every opportunity imaginable. I certainly did - but those initial bouts of
extreme cleverness pretty much worn off again once I found the plainer,
simpler (dare I say "duller"?) style made for easier and more durable code
in the long run.
I think as far as the to-nest-or-not-to-nest thing goes, follow your gut
and use whatever looks clearest and simplest and least likely to hurt your
brain. This is probably still not the right answer to your intended
question, mind you, but I hope it's at least a little less wide of the mark
than the last one...;)
HTH
has
_______________________________________________
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.