Re: System Events scripting of Finder window
Re: System Events scripting of Finder window
- Subject: Re: System Events scripting of Finder window
- From: kai <email@hidden>
- Date: Mon, 19 Mar 2007 02:40:38 +0000
On 18 Mar 2007, at 01:14, Doug McNutt wrote:
-- This, from kai, also works. His names were different from mine
probably because I'm stuck at 10.3.9. It's interesting to note
that "Arrange" is either menu item 5 or 6 depending on starting at
0 or 1 for the count from the top of the GUI pull down menu. I see
no way it can be 7, but it does work that way in the script above.
The count is being thrown by a couple of contextual menu items, Doug.
The first 3 items relate to the icons/list/column options, while item
4 is a menu item separator. Depending on Finder's selection status,
menu item 5 might be named something like "Clean Up" or "Clean Up
Selection". However, from a GUI scripting perspective, *both* items
are listed - so they actually take up positions 5 and 6. Since menu
item "Arrange" (or, in Tiger, "Arrange By") occurs next, it occupies
position 7.
To demonstrate this, try running the following short script:
----------------
tell application "System Events" to set {l, p} to ¬
{name, position} of menu items of menu 1 of ¬
menu bar item 5 of menu bar 1 of process "Finder"
tell l to repeat with i from 1 to count
if item i is missing value then
set item i to (i as string) & tab & "——————"
else if p's item i is missing value then
set item i to (i as string) & "*" & tab & item i
else
set item i to (i as string) & tab & item i
end if
end repeat
"Any item marked * is currently hidden:"
choose from list l with prompt result ¬
with empty selection allowed
----------------
I believe the AXRaise and frontmost commands are equivalent.
Applications and processes typically have a frontmost property, while
UI elements that might respond to a <perform action "AXRaise">
command include windows and scroll areas.
So either Finder or System Events could be used to make Finder the
frontmost application/process, and to also bring a specific window to
the front. In terms of effect, the following variants might therefore
be considered equivalent:
----------------
tell application "Finder"
activate
activate window "xyz" (* may not work in other applications *)
end tell
----------------
tell application "System Events" to tell process "Finder"
set frontmost to true
perform action "AXRaise" of window "xyz"
end tell
----------------
At 08:25 -0400 3/17/07, David Marshall wrote:
Try calling it menu bar 1.
Well taken. It shows as menu bar "" in the examiner. Sigh. I did
try putting the quoted null in.
I find that it sometimes helps to test such things using a short script:
----------------
tell application "System Events" to process "Finder"'s menu bars
--> {menu bar 1 of application process "Finder" of application
"System Events"}
----------------
Where possible, UI elements are normally referenced by name. So if
any are referenced by index, chances are that they have no name:
----------------
tell application "System Events" to name of process "Finder"'s menu bars
--> {missing value}
----------------
The following script may help initially in identifying menu
references. However, if anyone's likely to do this kind of thing on a
more serious/regular basis, then it might be worth considering Bill's
advice about PreFab UI Browser.
----------------
property default_ref : "Index"
to choose_item for t from l
set r to choose from list l's every Unicode text ¬
with prompt "Choose the required " & t & ":"
if r is false then error number -128
r's beginning
end choose_item
to get_index for t from l
set r to choose_item for t from l
tell l to repeat with i from 1 to count
if item i is r then return i
end repeat
end get_index
tell application "System Events" to tell process (my (choose_item for
"process" from ¬
name of processes where it is not background only)) to tell menu bar 1
set r to a reference to menu bar item (my (get_index for ¬
"menu bar item" from name of menu bar items))
repeat while exists r's menu 1
tell r's menu 1 to set r to a reference to menu item (my ¬
(get_index for "menu item" from name of menu items))
end repeat
end tell
set default_ref to button returned of (display dialog ¬
"Reference selected menu item by name or index?" buttons ¬
{"Cancel", "Name", "Index"} default button default_ref with icon 1)
if default_ref is "Index" then return r
r's contents
----------------
---
kai
_______________________________________________
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