Can anyone offer any advice on how to go about getting the titles of every menu item in an arbitrary contextual menu?
e.g.
Suppose we are dealing with TextEdit. I would like to be able to obtain a list of all of the menu items that are displayed when I Right-Click in the main Text Area.
Why? So that I can compare this list with a list of the menu items that I expect to find.
Some problems I have encountered:
- Contextual menus don’t appear within UI Browser (although you can invoke them using an object’s Actions).
- I haven’t had success with trying to contruct the widget hierarchy for an ‘arbitrary’ contextual menu. (From info gleaned using UI Element Inspector)
- I’ve tried code of the format:
set menutext to title of menu item of menu 1 of text area 1 of scroll area 1 of window “Untitled” as list
without success.
If anyone can offer any insight, advice, etc., then I would be very grateful. If I can provide more information I will be happy to.
tell application "TextEdit" to activate
tell application "System Events" to tell application process "TextEdit"
tell window 1 to tell scroll area 1
tell text area 1
perform action "AXShowMenu"
end tell
end tell
end tell
delay 5
NOTE: contextual menu disappears when the script ends and focus takes the Script Editor. So, I add some delay for the test purposes.
You can continue after perform action “AXShowMenu”, For example, clicking menu item “Services” of contextual menu:
click menu item "Services" of menu 1 -- of text area 1 of course
On my side I would use:
tell application "TextEdit" to activate
tell application "System Events" to tell application process "TextEdit"
set frontmost to true # Useful with 'old' systems
tell window 1 to tell scroll area 1
tell text area 1
set {{x, y}, {w, h}} to {position, size}
tell me to do shell script "/Users/admin/bin/cliclick kd:ctrl c:" & (x + w div 2) & "," & (y + h div 2)
tell first menu
name of menu items
--> {"Couper", "Copier", "Coller", missing value, "Police", "Orthographe et grammaire", "Substitutions", "Transformations", "Parole", "Orientation de la mise en page", "Services"}
end tell
end tell
end tell
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 11 aout 2019 12:36:15
Yes, it is better to get all names of menu items instead of menu items themselves, but no need click-click here:
tell application "TextEdit" to activate
tell application "System Events" to tell application process "TextEdit"
tell window 1 to tell scroll area 1
click
tell text area 1
perform action "AXShowMenu"
set menuTemsNames to name of menu items of menu 1
end tell
end tell
end tell
-- {"Look Up “version”", "Search With Google", missing value, "Cut", "Copy", "Paste", missing value, "Share", missing value, "Font", "Spelling and Grammar", "Substitutions", "Transformations", "Speech", "Layout Orientation", "Services"}
Thanks.
I don’t understand why, when I tried to get the name of menuIetms after issuing perform action “AXShowMenu”, I got nothing.
Now it works.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 11 aout 2019 14:36:43