Wednesday, November 20, 2019

#1 2006-03-07 02:25:58 pm

dprescott
Member
Registered: 2006-03-03
Posts: 1

Get titles of menu items in a contextual menu

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:
1) Contextual menus don't appear within UI Browser (although you can invoke them using an object's Actions).
2) I haven't had success with trying to contruct the widget hierarchy for an 'arbitrary' contextual menu. (From info gleaned using UI Element Inspector)
3) 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.

Offline

 

#2 2019-08-11 03:04:15 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 714

Re: Get titles of menu items in a contextual menu

Applescript:


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:

Applescript:

click menu item "Services" of menu 1 -- of text area 1 of course

Last edited by KniazidisR (2019-08-11 03:29:27 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#3 2019-08-11 04:37:04 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: Get titles of menu items in a contextual menu

On my side I would use:

Applescript:

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

Offline

 

#4 2019-08-11 05:44:47 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 714

Re: Get titles of menu items in a contextual menu

Yes, it is better to get all names of menu items instead of menu items themselves, but no need click-click here:

Applescript:


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"}

Last edited by KniazidisR (2019-08-11 05:58:55 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#5 2019-08-11 06:36:51 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: Get titles of menu items in a contextual menu

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)