Getting MenuItem DisplayValue with Applescript - applescript

Using Applescript, I would like to retrieve all the display value of the menu items of the Help menu.
So far I have:
#!/usr/bin/osascript
on run {appName, myCmd}
tell application "System Events" to tell process "Chrome"
set frontmost to true
set myMenu to menu bar item "Help" of menu bar 1
set allUIElements to entire contents of myMenu
repeat with anElement in allUIElements
log name of anElement
end repeat
end tell
end run
which returns:
name of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of static text Search of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of text field Search of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 1 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of UI element 1 of row 1 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of static text 1 of UI element 1 of row 1 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of UI element 2 of row 1 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of image 1 of UI element 2 of row 1 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of static text 1 of UI element 2 of row 1 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 2 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of UI element Menu Items of row 2 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of static text Menu Items of UI element Menu Items of row 2 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of UI element Import Bookmarks and Settings… of row 2 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of image 1 of UI element Import Bookmarks and Settings… of row 2 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of static text Import Bookmarks and Settings… of UI element Import Bookmarks and Settings… of row 2 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 3 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of UI element 1 of row 3 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of UI element 2 of row 3 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 4 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 5 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 6 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 7 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 8 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 9 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 10 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 11 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 12 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 13 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 14 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 15 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 16 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 17 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 18 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 19 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 20 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of row 21 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of column 1 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
name of column 2 of table 1 of menu item 1 of menu Help of menu bar item Help of menu bar 1 of application process Google Chrome
would there be a way to only retrieve the display value?
In my case I would like to outout:
Import Bookmark and Set ...
Always Show Bookmarks Bar
Bookmark Manager
[...]
Customize the Finder tool..
Set up parental controls
Show all Help Topics

Most likely not the best solution, but the following seems to be working:
repeat with anElement in allUIElements
set className to class of anElement
set displayValue to name of anElement
if (className as string) is equal to "static text" then
if (displayValue as string) is not equal to "missing value" then
log displayValue
end if
end if
end repeat
Let me know if you know a better way

Related

Applescript Click item in system menu bar after opening it

I can open the bluetooth dropdown menu item with this code but I don't know how to actually click on any of the items in the menu.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
end tell
What would be a command to click on something in the open bluetooth menu?
This following AppleScript code should accomplish what you are trying to achieve. Just replace the "Mac Pro" part of the code with the name of the item you want to click.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
click checkbox "Mac Pro" of scroll area 1 of window 1
key code 53 -- Press escape key
end tell
This following AppleScript code will return the names of the checkboxes so you can easily know your options to use in the first code.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
set checkBoxNames to name of checkboxes of scroll area 1 of window 1
end tell

How can I choose a menu item from dropdown menu using AppleScript (Big Sur OS)

For Catalina I would only do something like this in Preview
click pop up button 3
click menu item "Study Edge" of menu 1 of pop up button 3
But for Big Sur this no longer works :(
After adding splitter group which is now needed for Big Sur my code looks as follows:
tell sheet 1
tell splitter group 1
click pop up button 3
click menu item "Study Edge" of menu 1 of pop up button 3
end tell
click menu button "PDF"
repeat until exists menu 1 of menu button "PDF"
delay 0.02
end repeat
click menu item "Save as PDF" of menu 1 of menu button "PDF"
end tell
But it is not able to find the menu item "Study Edge"

How to use AppleScript check if a menu item checked

see my image
I want to get the value of the menu item "Set as system proxy" (whether if it has been checked).
The problem is if I want to get that value I have to click that menu first and find the menu item. But I want it to be done in the background since I want it to be executed every 5 seconds.
My code goes like this
tell application "System Events"
tell process "ClashX"
click (menu bar itm 1 of menu bar 2)
get value of attribute "AXMenuItemMarkChar" of menu item "Set as system proxy" of menu 1 of menu bar item 1 of menu bar 2
end tell
end tell
(^^^^ This opens that menu over and over again)
and when I delete the line "click (menu bar itm 1 of menu bar 2)", which is
tell application "System Events"
tell process "ClashX"
get value of attribute "AXMenuItemMarkChar" of menu item "Set as system proxy" of menu 1 of menu bar item 1 of menu bar 2
end tell
end tell
the script cannot be done, error "System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process "ClashX". Invalid index." number -1719 from menu 1 of menu bar item 1 of menu bar 2 of process "ClashX"
If you want to know whether or not the Set as system proxy menu item is checked for ClashX, you can check the value of the proxyPortAutoSet key in its preferences plist file, e.g.:
In Terminal:
defaults read com.west2online.ClashX 'proxyPortAutoSet'
Returns 1 when checked, and 0 when not checked.
If you want to use it in AppleScript, use a do shell script command, e.g.:
set menuItemIsChecked to ¬
(do shell script ¬
"defaults read com.west2online.CrashX 'proxyPortAutoSet'") ¬
as boolean
if menuItemIsChecked then
# Your code goes here.
end if

Applescript to click checkbox in cell in row in outline

I have a checkbox in the following hierarchy:
Outline->Row->Cell->Checkbox
This is how it looks in accessibility inspector:
I need to click on it.
So, I tried:
click checkbox 1 of cell 1 of row 3 of outline 1 of scroll area 2 of splitter group 1 of tab group 1 of window 1
This gives the error:
Expected end of line, etc. but found number.
If I try to select the row like this:
select row 3 of outline 1 of scroll area 2 of splitter group 1 of tab group 1 of window 1
The row is getting selected.
Also, I can read the properties of the row like so:
get properties row 3 of outline 1 of scroll area 2 of splitter group 1 of tab group 1 of window 1
but not the properties of the checkbox/cell using:
get properties of checkbox 1 of cell 1 of row 3 of outline 1 of scroll area 2 of splitter group 1 of tab group 1 of window 1
I'm an absolute n00b at applescript, I have no, I idea what is going wrong.
Can anyone help?
PS: I am trying to turn off the screenshot shortcuts with in system preferences.
It's choking on the specifier for AXCell. Cell doesn't work. Just use UI Element 1:
tell application "System Events"
tell process "System Preferences"
click checkbox 1 of UI element 1 of row 3 of outline 1 of scroll area 2 of splitter group 1 of tab group 1 of window 1
end tell
end tell

how to get the context menu after perform AXShowMenu in AppleScript

my code like this
tell button 1 of window 1
perform action "AXShowMenu"
delay 0.2
end tell
after the menu is shown, how to get the menu? many thanks
It's the items of menu 1 of the drop down button:
tell button 1 of window 1
click
delay 0.2
get every menu item of menu 1
--click menu item "name" of menu 1
end tell

Resources