Re: Tell Blocks Considered Harmful (was Re: open for access)
Re: Tell Blocks Considered Harmful (was Re: open for access)
- Subject: Re: Tell Blocks Considered Harmful (was Re: open for access)
- From: Chris Page <email@hidden>
- Date: Tue, 16 Dec 2008 22:03:13 -0800
Hold onto your hats, folks!
On Dec 16, 2008, at 9:05 PM, Rick Gordon wrote:
I want to get the efficiency of not burdening the calling
application with OSAX calls, and get the effect of the second
snippet without the verbosity.
Use “tell me to display dialog ...” instead of “my (display
dialog ...)”.
Or you can be more explicit and say “tell current application to
display dialog ...”. I'm using “me” for brevity, but it doesn't mean
the same thing as “current application”.
“tell me to ...” means: Send this command to the current script, then
if it isn't handled send it to the parent script of the script, then
to its parent, and so on, until some script handles it or you get to
the current application. “tell current application to ...” means: Tell
the current application to perform the command, skipping any
intervening scripts.
Why doesn't the first snippet work like the second, if the "my"
pulls the scope out of the application domain?
“my” (and “of me”) are not identical to “tell me to”. You can't “tell”
within an expression (because “tell ... to ...” is a statement), so I
use “my” in that case, for brevity, but you can't use it in all cases
to send addition commands to the script.
“my” only works when the command doesn't have a direct object. The
direct object is the argument immediately following a command name and
it normally determines where a command is sent. “choose file” doesn't
have a direct object. It has other parameters, but there is no
argument immediately following the command name. “display dialog”,
however, does have a direct object, the message string.
“tell” really means “set the default target”. The default target is
stored in a read-only variable named “it”. The default target is used
whenever the direct object of a command is a partial object specifier
or a value that is not a specifier. “it” is added to the end of
partial specifiers. The completed object specifier determines the
target of the command.
“my” (and “of me”) mean something like “send the message to me, the
script”. In the case of a scripting addition command, the message is
“send this command to the direct object”. That message is sent to the
script, and then the script handles it by sending the command to the
direct object. (Approximately.) Most importantly, “my” doesn't affect
“it” in the same way as “tell”, so it doesn't affect the direct object.
So
tell application "Finder" to display dialog "Hi!"
really means
set it to application "Finder" -- not actual code, because “it” is
read-only
display dialog "Hi!"
Since “display dialog” has a direct object, and it is not a complete
object specifier (in fact, it's not a specifier at all, it's a
string), the command is sent to “it” -- to Finder. But if we write
tell application "Finder" to display dialog application "System
Events"
the dialog command is sent to System Events, because it is the direct
object. “it” is ignored when the direct object is a complete object
specifier that ends with an application. (The command results in an
error, by the way, because ‘application "System Events"’ isn't a
string.)
In summary, use “tell me to ...” or “tell current application to ...”
to be sure to send a command to the current application running the
script. Use “my (...)” or “(... of me)” only when the command doesn't
have a direct object and you want to be brief.
--
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