Re: Referencing "unknowns" with UI scripting
Re: Referencing "unknowns" with UI scripting
- Subject: Re: Referencing "unknowns" with UI scripting
- From: kai <email@hidden>
- Date: Tue, 28 Feb 2006 00:39:27 +0000
On 27 Feb 2006, at 09:15, kai wrote:
On 27 Feb 2006, at 03:41, email@hidden wrote:
ps. After trying to script the Page Setup window of Notes I
realised that
the options for the destination printer, paper size and
orientation are
not accessible even with UI scripting. I even tried looking at it
from
inside Safari, thinking that perhaps just Notes was useless but it
seems
to be an OS thing, not just app specific.
Printing is such a basic function, surely Apple would include
these things
for scripting... at least with UI!!!
If you're having difficulty registering clicks, and don't feel like
waiting for the remaining wrinkles in the UI scripting
implementation to be ironed out, check out Extra Suites...
I've now had a brief opportunity to take a closer look at this, Coj.
No wonder you're frustrated. This situation is a bit like the one we
encountered the other day - to which Bill responded with
characteristic insight:
http://lists.apple.com/archives/applescript-users/2006/Feb/msg00490.html
By way of experiment, I tried targeting some similarly afflicted
popup buttons in Safari's Page Setup sheet. Part of the problem is
that, while Accessibility Inspector offers details of button position
and size, even these aren't available to a script - presumably for
the same reasons that Bill kindly mentioned. And if we can't get a
bead on the target, how can we be expected to land a resounding click
on the darned thing - even with the help of third-party utilities,
such as the aforementioned Extra Suites?
And even if we manage that - what hope is there, without positional
feedback, of accurately navigating a list of menu items? (Ever tried
flying a B-52 under the Brooklyn bridge, upside-down, in a force 9,
at night - and wearing a blindfold? No - really?)
As far as I can see (which isn't too far, these days), there's no
elegant way around this. But if you _really_ get stuck for a way
forward, I'll recklessly abandon any of my few remaining principles -
and offer one of my clunkiest* suggestions to date.
(* More years ago than I care to admit, we had a long-running road
safety campaign in the UK, promoting the use of car seat belts. The
oft-quoted slogan was "clunk, click - every trip!" Now why, after all
this time, does that phrase keep springing to mind - whenever I
contemplate embarking on another UI scripting adventure?) ;-)
Anyway, I digress...
There's at least one useful piece of information that we can glean
from System Events: the current position of the target window/sheet.
With a bit of homework, we could also calculate the location of
various target objects - relative to the sheet itself. When running a
script, this should help us to home in on each element - regardless
of where the window might be at the time. (The good news is that
Extra Suites can help here, too - since its 'ES mouse location'
command is useful for pinpointing the centre of an object.)
But wait - it gets worse. Once over that hurdle, there's still the
thorny question of how to select menu items. Since we have no
positional or textual information to guide us here, the best we can
probably do is to feel our way around the menu, using keystrokes.
Hopefully, we should already know the index position of the required
item - as well as the total number of menu items. Easy.
Um... except that, when a popup is selected, there's no way for the
script to tell which menu item was already selected - and that's our
starting point. (In other words, we know how many steps there are
between points A and B. We just won't know whether we're at point A,
B, C or... Z.) So we may have to resort to groping towards one end of
the menu or the other, and then backtracking - counting as we go.
(Think of this as the UI scripting equivalent of bumping into walls
as a navigational technique. Sort of like RADAR - but very close range.)
All a bit Heath Robinson? Perhaps.
But it seems to work surprisingly well here - not to mention
consistently. As mentioned, I tested with Safari's Page Setup sheet
by attempting to change the paper size to A3. OMM, one of 14 (from a
total of 15) menu items might be selected. (With custom options, this
figure could be higher.) The A3 option is menu item 12. Here's the
script I used to select it:
--------
(* requires application "Extra Suites" *)
to click_mouse at p
tell application "Extra Suites"
ES move mouse p
ES click mouse
end tell
end click_mouse
to select_item at pos from tot
tell application "System Events"
repeat tot - 1 times
key code 126
end repeat
repeat pos - 1 times
key code 125
end repeat
keystroke return
end tell
end select_item
tell application "Safari" to activate
tell application "System Events" to tell process "Safari"
tell menu item "Page Setup…" of menu "File" of ¬
menu bar item "File" of menu bar 1
if not enabled then error number -128
click
end tell
tell sheet 1 of window 1
repeat until exists
delay 0.1
end repeat
set {leftEdge, topEdge} to position
end tell
end tell
tell leftEdge + 245 (* translate into button positions *)
set settingsBtn to {it, topEdge + 30} (* not used in this example *)
set formatBtn to {it, topEdge + 75} (* ditto *)
set paperSizeBtn to {it, topEdge + 125}
end tell
click_mouse at paperSizeBtn (* call relevant menu *)
delay 0.1 (* give it a moment to settle *)
select_item at 12 from 14 (* select required item from total
selectable items *)
tell application "System Events" to keystroke return (* close sheet *)
--------
"But", I hear you say, "it shouldn't be like this!"
I agree. :-)
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden