Re: OSAX within tells
Re: OSAX within tells
- Subject: Re: OSAX within tells
- From: Cal <email@hidden>
- Date: Thu, 18 Jan 2001 16:15:05 -0500
email@hidden writes:
>>>>And, it is not necessary - and may sometimes be a bad idea - to
encapsulate
a call to a Scripting Addition in a "tell" wrapper.
While it is not necessary, there are very very few cases where it is a bad
idea.
Osax were designed to work very well with AppleScript and conflicts between
Osax and applications are very rare and often exagerated.
IMHO it's a worse idea to try to write scripts with Osax calls outside of
tell blocks.
And:
>>>>Umm.... Why?
Because it clutters up and breaks up perfectly readable and understandable
AppleScripts for no good reason.
And that makes scripting more complex and tedious and harder to learn.
It also prevents you from using a wide range of compound commands like:
set the name of myFile to date string for (current date)
In the keyboard example given there is no reason not to use ASCII Character
or ASCII Number commands within a finder tell or a quark tell or
Photoshop/PhotoScripter tell. It doesn't significantly impact processing
time (and in some cases may even be slightly faster.) I haven't heard of a
single bug with either of those or with most of the commonly used OSAX
commands or even with any of the more rare ones.
Including OSAX commands within application tells is not a "bad habit".
It's using the technology the way it was designed. I believe that all the
AppleScript books agree and nearly every sample script from Apple that I've
seen uses them.
Now, I'm not saying there is anything wrong with moving commands outside
tells if your more comfortable with that and prefer that, but I am saying
we should not tell Scripters, particularly new Scripters, that it's a
problem when it's not.
Phi Sanders <email@hidden> replied:
Ahhh, I see what your saying...
Don't put OSAX calls OUTSIDE tell blocks if you are in the middle
of telling an application some multi-step task
tell app "foo"
do this
--the next call is to an osax
do osaxthing
-- now "tell" to "foo" some more
do that
end tell
as opposed to :
tell app "foo"
do this
end tell
--the next call is to an osax
do osaxthing
-- now "tell" to "foo" some more
tell app "foo"
do that
end tell
Hmmm...I wouldn't be too hasty with that advice.
Placing osax commands inside tell blocks has certain effects, some of
which may not be desirable:
- Anything that generates an interapplication message (Apple event)
inside a tell block -- including commands for scripting additions --
will generate an inter-process call, involving the Process Manager.
This may slow down the execution of your script.
- Anything that generates an interapplication message (Apple event)
inside a tell block -- including commands for scripting additions --
will most certainly involve the event loop and at least some code in
the target application. In addition to the possibility of slowing
down the execution of your script, applications may do different (and
occasionally unexpected) things when handling the message, before it
even gets to the scripting addition.
The first case in Phi's example will always cause more code to be
executed than the second case. Whether the difference is noticable
will depend on the efficiency of the target application's event
handling, and how many repetitions the scripting addition will be
called. If you're doing thousands of executions of the "offset"
command or the "info for" command, you'll likely notice the
difference. OTOH, calling "beep" or "current date" will work fine.
- Some scripting addition commands (such as "display dialog") will
cause user interaction in that application. If that application
isn't frontmost, depending on the behavior of the particular
scripting addition, the user may have to bring that application to
the front to see the dialog box; in other cases, the dialog box will
appear in the background (not a Good Thing). OTOH, you may want to
do this because the user of your script will already have that
application in the foreground, so there won't be a problem.
But...remember that, if your script is executing as an application
(applet or droplet), it will run faster if the script itself is the
frontmost application (presuming that the target application will
work properly or efficiently if it's not frontmost; this will vary
from application to application).
- The possibility for terminology conflicts increases greatly when a
scripting addition's vocabulary is being compared with an
application's vocabulary, as opposed to a scripting addition's
vocabulary being compared with just AppleScript vocabulary. Remember
that specifying a tell block says to AppleScript, "Add these terms to
your language for a few moments." Developers of scripting additions
are fairly good about making an effort to stay clear of vocabulary in
other scripting additions (particularly the commonly-used ones), but
they cannot be expected to steer clear of all the tens of thousands
of terms in every scriptable application (PhotoScripter has over 2000
terms). I see conflicts of this nature just about every week.
So I'd give the following advice: Be cautious of calling scripting
additions in tell blocks, unless a) you know the behavior of the
target application, b) if that application will already be in the
foreground, and c) if you're only executing the command a small
number of times. (I'm not saying never to do this, but having an
arbitrary guideline like "it's good" or "it's bad" won't wash; you
should consciously have a good reason for doing it in each specific
case. To avoid problems, it's generally better to place calls to
osaxen outside tell blocks unless you want a specific effect, like
displaying a dialog in that application.)
To clarify, ending and then starting tell blocks does not affect the
efficiency of your scripts:
tell application "Finder"
count the disks
count the folders
end tell
runs exactly the same as:
tell application "Finder"
count the disks
end tell
tell application "Finder"
count the folders
end tell
and the same as:
tell application "Finder" to count the disks
tell application "Finder" to count the folders
Cal