Applescript hurdle I can't quite get past...

steve harley steve at paper-ape.com
Mon Mar 12 01:51:58 PDT 2007


they whom i call Dan Lowe wrote:
> [UI scripting actions attempting to access submenu of PDF button]
 >
> But, nothing seems to work. I get various errors when I run this in 
> Applescript Editor, always at the PDF button step.
> 
> Any help will be appreciated. I have this feeling that I am not seeing 
> the forest for the trees.

it would be nice if there were a forest, instead, Apple has left 
a few snags in a clearcut; i poked at this, and it's like 
numerous other UI scripting roadblocks: the element you want 
doesn't seem to be implemented in a way UI scripting can access 
... at least by the expected means

before revealing the workaround, first my inspection technique to 
decode the hierarchy of elements:

float around with the UI Element Inspector and you'll notice that 
there are two "sub-panels" on the iCal Print dialog, the lower 
one with the PDF menu button is UI element 5 of the window; you 
can confirm this with an interactive query:

   UI elements of UI element 5 of window "Print"

-->	{ button 1 of UI element 5 of window "Print" of application 
process "iCal",
			UI element 2 of UI element 5 of window "Print" of application 
process "iCal",
			button "Print" of UI element 5 of window "Print" of 
application process "iCal",
			button "Cancel" of UI element 5 of window "Print" of 
application process "iCal",
			button "Preview" of UI element 5 of window "Print" of 
application process "iCal",
			UI element 6 of UI element 5 of window "Print" of application 
process "iCal"
		}

sure enough, Print, Cancel and Preview are in there along with 
three unspecific items, one of which must be the PDF button; use 
the inspector to note the position of the PDF button, then run 
this query and look for a corresponding position in the event log:

   tell UI element 5 of window "Print"
     repeat with i from 1 to count of UI elements
       position of UI element i
     end repeat
   end tell

this shows that element 6 is the PDF button, though the position 
is off by three pixels; this is your handle on the button, but 
what about the menu item?

the inspector shows the button's children to be an array of zero 
elements, yet when you inspect the menu items themselves, the 
inspector reveals that the hierarchy includes an "AXMenu" element 
between the menu button and the menu items; there lies the rub -- 
the AXMenu lurks out of reach of the script, preventing the 
script from directly selecting menu items; instead, it must click 
the button by its obtuse reference, then move to the menu item:

   click UI element 6 of UI element 5 of window "Print"
   key code 125 -- down arrow to the Save As PDF... menu item
   delay 0.2
   keystroke return -- select the menu item

note that i've seen others resort to deriving the {x,y} position 
of the button based on the window position and then using the 
"click at" command




More information about the MacOSX-talk mailing list