tell application "Address Book" to activate
tell application "System Events"
tell process "Address Book"
click menu item 12 of menu 3 of menu bar 1
delay 1
tell sheet 1 of window 1to click button 1
end tell
end tell
Yvan KOENIG (from FRANCE mardi 3 octobre 2006 11:43:48)
Have you tried adding a delay to make sure the app is active before the keystroke happens?
tell application "Address Book" to activate
delay 1 --try different settings but 1 is a good start
tell application "System Events"
keystroke "b" using command down
dalay 0.5
keystroke return
end tell
end tell
tell application "Address Book" to activate
tell application "System Events"
tell process "Address Book"
click menu item 12 of menu 3 of menu bar 1
delay 1
tell sheet 1 of window 1 to click button 1
end tell
end tell
(and did NOT work for ical)
but this one worked perfectly for ical:
tell application "iCal" to activate
tell application "System Events"
tell process "iCal"
keystroke "b" using command down
delay 1
tell sheet 1 of window 1 to click button 1
end tell
end tell
(and did NOT work for address book…)
however, neither using the above “tell sheet 1…” nor “keystroke return” worked in camino. i looked throught everything in UI Element Inspector and there’s nothing different there.
oops, no sorry, this is after i’ve already gotten to as save as dialog (specifically the export bookmarks).
all i need to do is keystroke return, to press the “export” button in that dialog
note that in another application, Delicious Library, using “keystroke return” worked just fine for the resulting ‘do you want to replace the existing one’ window, as seen here:
tell application "Delicious Library" to activate
tell application "System Events"
tell process "Delicious Library"
keystroke "e" using command down
delay 1
tell sheet 1 of window 1 to click button 1
delay 1
keystroke return
end tell
end tell
so it seems like getting the return key to click in applescript works differently in different apps?
whomever might be able to clear this up… i spell that H-E-R-O
thought it does seem kind of wak of apple to make it so confusing…
Each application has its own menus so, the instruction calling one of them is specific to itself.
tell application "Address Book" to activate
tell application "System Events"
tell process "Address Book"
click menu item 12 of menu 3 of menu bar 1
delay 1
tell sheet 1 of window 1 to click button 1
end tell
end tell
tell application "iCal" to activate
tell application "System Events"
tell process "iCal"
click menu item 10 of menu 3 of menu bar 1
delay 1
tell sheet 1 of window 1 to click button 1
end tell
end tell
For an other application the num of the menu item to click would be different.
No mystery here.
Yvan KOENIG (from FRANCE mardi 3 octobre 2006 22:42:10)
Monsieur Koenig is correct - Apple doesn’t control how menus and buttons are referenced, the authors of the applications do. There are no ‘standard’ ways.
Is it really difficult to count the menu items in a menu?
Just remember that a divider line counts as one item.
I often use this trick:
tell application "Address Book" to activate
tell application "System Events"
tell process "Address Book"
get name of every menu item of menu 3 of menu bar 1
(*
click menu item 12 of menu 3 of menu bar 1
delay 1
tell sheet 1 of window 1 to click button 1
*)
end tell
end tell
Gonna second this idea. What a godsend this little utility is for some of the truly odd ways some applications refer to windows, menus, buttons, popups, etc.
EDIT FOR KOENIG:
I can only assume you’ve had really good luck doing the “count manually” routine. So far I’ve bumped into at least two applications where this doesn’t work, oddly enough. One is the print dialog of the Adobe CS suite, the other is Lotus Notes. For me, that makes a 50/50 split between the “easy” ones and the “my Goddess I’m glad I had Prefab’s little utility.”
Unfortunately, in the same vein that Apple doesn’t control how the UI elements are numbered or referenced, they don’t seem to have alot of control over even folks like Adobe in trying to get them to “play nice” for folks like us.
Alternatively, you could use a routine I wrote the other day that walks you through the hierarchy of UI elements for a given app (if it DOES follow the standard UI Applecadabra).
tell application "System Events"
set fApp to name of some application process whose frontmost is true and background only is false
set Appz to name of application processes
tell application fApp
activate
set appn to choose from list Appz with prompt "Select app" OK button name "OK"
end tell
if appn is false then return
set targName to item 1 of appn
set theApp to targName
set mainTarget to application process targName
repeat
set fApp to name of some application process whose frontmost is true
tell process fApp to activate
set UIlist to (get mainTarget's UI elements)
set UInames to {}
repeat with h from 1 to number of items in UIlist
try
set name1 to (get name of item h of UIlist)
if name1 = "" then set name1 to ("itempje " & h) as string
on error
set name1 to (class of item h of UIlist & " " & h) as string
end try
set UInames to UInames & {name1}
end repeat
if UInames = {} then exit repeat
if number of items of UInames = 1 then
set selname to item 1 of UInames
else
set selname to (choose from list UInames with prompt "UI elements of \"" & targName & "\": " default items 1 OK button name "Browse") as string
if selname is "false" then exit repeat
end if
set itemNo to my getIndex(selname, UInames)
set mainTarget to item itemNo of UIlist
try
set targName to name of mainTarget
if name of mainTarget = "" then set targName to fApp
on error
set targName to fApp
end try
end repeat
end tell
repeat until fApp = theApp
tell application theApp to activate
tell me to do shell script "sleep 1"
tell application "System Events" to set fApp to name of some application process whose frontmost is true
end repeat
tell application "System Events" to click mainTarget
on getIndex(i, l)
repeat with n from 1 to count l
if l's item n is i then return n
end repeat
0
end getIndex
CalvinFold is right.
This is why sometimes I don’t directly use an hardcoded item index but grab it from an item which is more easily identified.
I met an application which was canging the number of items in a menu giving a specific environment.
I was able to get the correct index by grabbing the total number of menu items or by grabbing the index of the first separator.
Of course, I would have preferred to be able to rely on the “localized string” function but Chris Nebel explained that it is not done for this kind of task and that the menu definitions are not stored in the Localizable.strings file and not even in a complementary xxxxx.strings file. In fact they are stored in nib files which are not deciphered by the “localized string” tool.
Of course, when someone write a script for its own use, taking care of localized versions is not important but, when a script is posted worldWide, I think that it is correct to take care of the problem.
Yvan KOENIG (from FRANCE samedi 7 octobre 2006 13:35:18)