Re: application and application (was: System Events: window id's instead of window names?)
Re: application and application (was: System Events: window id's instead of window names?)
- Subject: Re: application and application (was: System Events: window id's instead of window names?)
- From: Axel Luttgens <email@hidden>
- Date: Thu, 24 Jun 2010 12:37:13 +0200
Le 23 juin 2010 à 11:21:37, Thomas Fischer a écrit :
> [...]
>
> Basically I agree, but checking I came about a puzzling effect:
>
> set appName to "AppleScript Editor"
> tell application "System Events" to tell application appName to get id of window 2
>
> doesn't work, although
>
> set appName to "AppleScript Editor"
> tell application appName to get id of window 2
>
> and
> tell application "System Events" to tell application "AppleScript Editor" to get id of window 2
>
> do.
> So there is some difference in the evaluation of variables. Any idea?
Hello Thomas,
This is more a compiler and scoping matter.
Let's have a look at this very basic construct:
tell application "Finder"
id
--> "com.apple.finder"
end tell
Here, the compiler recognizes following sequence:
application <constant string>
and may unambiguously "decide" that one is targeting the Finder application object. This is a desirable behavior, since it wouldn't otherwise be possible to write such constructs (even if they should be used with moderation):
tell application "Finder"
tell application "Dock"
id
--> "com.apple.dock"
end tell
end tell
Now, let's try this one:
set appName to "Dock"
tell application "Finder"
tell application appName
id
--> error number -1728 from application "Dock"
end tell
end tell
Here, the compiler notices this sequence
application <variable>
and thus faces some ambiguity: <variable> may potentially be anything, and at the same time the compiler "sees" that the keyword "application" appears in the Finder's dictionary; so, perhaps does the Finder know how to handle such a construct? Obviously, it doesn't... ;-)
It is interesting to have a look at the Event Log after having run each of the above snippets, especially wrt to the consecutive targets and the varying meaning of the keyword "application" depending on the context.
One way to solve the problem is to bypass the Finder as target, and to instead address AppleScript itself:
set appName to "Dock"
tell application "Finder"
tell me
tell application appName
id
--> "com.apple.dock"
end tell
end tell
end tell
This also explains why
set appName to "Dock"
tell application appName
id
--> "com.apple.dock"
end tell
works as expected: there's no interfering dictionary, and the meaning of the keyword "application" is thus the one provided by AppleScript, even if not followed by a constant string.
HTH,
Axel
_______________________________________________
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