How to click on static text in applescript - applescript

enter image description here
I have the window of the picture above, save the configuration through stand-alone save, AppleScript can not click Save through click, the following script returns missing value, please ask in AppleScript, how to click static text
tell application "System Events"
tell process "WeChat"
click menu item "Proxy Settings..." of menu "WeChat" of menu bar 1
click radio button "Don't Use" of window "Proxy Settings"
delay 1
set focused to true
click static text "Save" of window "Proxy Settings"
end tell
end tell
```[enter image description here](https://i.stack.imgur.com/ZWJ9V.png)
tell application "System Events"
tell process "WeChat"
click menu item "Proxy Settings..." of menu "WeChat" of menu bar 1
click radio button "Don't Use" of window "Proxy Settings"
delay 1
set focused to true
click static text "Save" of window "Proxy Settings"
end tell
end tell

I don't have/use WeChat, so cannot examine the UI to confirm here, but static text elements—generally speaking—don't often have actions attached to them ("AXPress" is the name of the action that the click command aliases). From a design perspective, static text elements are intended as an inert label for some other UI element that plays a functional role. In this case, it looks like a button element, and its name is probably also "Save". button elements are also good candidates for attaching a whole bunch of actions to.
Therefore, instead of:
click static text "Save" of window "Proxy Settings"
you could try:
click button "Save" of window "Proxy Settings"
If that throws an error, it'll either be because the button element's name is not "Save", or the element is not a button. But you can find out what it is like this:
-- click button "Save" of window "Proxy Settings"
tell window "Proxy Settings" to get (the last UI element ¬
where the name of its actions contains "AXPress")
This will yield a reference to the object you're trying to click, by virtue of its actions necessarily containing one named "AXPress" (i.e. it responds to the click command), and its physical positioning almost certainly inferring it to be the last such UI element.

"Save" is not a button, and only one button can be found through the UI element, this button is a close button, and nothing else.

Related

AppleScript to make the display black and white for monteray

Can you rewrite this applescript for Monterey?
tell application "System Preferences" to reveal anchor "Seeing_ColorFilters" of pane "Accessibility"
tell application "System Events"
tell process "System Preferences"'s first window's first group's first tab group
click checkbox "Enable Color Filters"
tell first pop up button
click
-- if necessary, uncomment the following line to allow time for the menu to open
-- delay 0.1
tell its menu
click menu item 1
end tell
end tell
end tell
end tell
tell application "System Preferences" to quit
This code is a script that makes the display grayscale. I used to use this code in BigSur. However, after updating the version to Monterey, it stopped working. My Monterley version is 12.6.2.
Thanks,
The problem appears to be a timing issue. The menu that your script uses to select the greyscale option is no longer immediately available under Monterey 12.6.2. A short delay is required.
I took this script and enabled it one line at a time. With the System Preferences window already open and the color filters pane revealed, it worked all the way to the end.
So, I then ran the full script with System Preferences closed. It failed at the line:
click checkbox "Enable Color Filters"
with the error:
Script Error
System Events got an error: Can’t get group 1 of window 1 of process "System Preferences". Invalid index.
That error sounds like the window isn’t yet available. So I tried adding a delay in front of clicking the checkbox:
delay 0.1
click checkbox "Enable Color Filters"
The script then began to work as I assume you wished it to: the checkbox is checked, the first item on the menu (Greyscale) is selected, and the screen goes greyscale.
However, a hard-coded delay seems likely to fail if the system takes variable amounts of time to make the window available. So, rather than hardcode the delay, I added a loop to check for the existance of the desired checkbox:
repeat while not (exists checkbox "Enable Color Filters")
delay 0.1
end repeat
click checkbox "Enable Color Filters"
This version should work in past and future operating systems even if the timing changes, as long as the terminology remains the same.
The same technique should also work for your commented-out delay that waits for the menu to become available:
tell first pop up button
click
-- if necessary, allow time for the menu to open
repeat while not (exists its menu)
delay 0.1
end repeat
tell its menu
click menu item 1
end tell
end tell
Currently, the script toggles between greyscale and no filtering. That is, if the checkbox is already checked, this script will uncheck it. If your purpose is to ensure that the screen is always in greyscale, as your question implies, you may wish to verify the value of the checkbox first:
if the value of checkbox "Enable Color Filters" is 0 then
click checkbox "Enable Color Filters"
tell first pop up button
click
-- if necessary, allow time for the menu to open
repeat while not (exists its menu)
delay 0.1
end repeat
tell its menu
click menu item 1
end tell
end tell
end if
Checkboxes have a value of 0 or 1, depending on whether they’re currently checked (1) or unchecked (0).
If, on the other hand, your goal is for the script to toggle between the two states, there’s no reason to play around with the menu if you’re toggling color filters off:
if the value of checkbox "Enable Color Filters" is 0 then
click checkbox "Enable Color Filters"
tell first pop up button
click
-- if necessary, allow time for the menu to open
repeat while not (exists its menu)
delay 0.1
end repeat
tell its menu
click menu item 1
end tell
end tell
else
click checkbox "Enable Color Filters"
end if
Assuming the purpose is to guarantee the screen is in greyscale, the full script might end up looking like this:
tell application "System Preferences" to reveal anchor "Seeing_ColorFilters" of pane "Accessibility"
tell application "System Events"
tell process "System Preferences"'s first window's first group's first tab group
-- if necessary, allow time for the checkbox to become available
repeat while not (exists checkbox "Enable Color Filters")
delay 0.1
end repeat
--ensure that greyscale filters are enabled
if the value of checkbox "Enable Color Filters" is 0 then
click checkbox "Enable Color Filters"
tell first pop up button
click
-- if necessary, allow time for the menu to open
repeat while not (exists its menu)
delay 0.1
end repeat
tell its menu
click menu item 1
end tell
end tell
end if
end tell
end tell
tell application "System Preferences" to quit

AppleScript: problem after upgare to Ventura: buttonnot found

I have a simple script that automates the command "Export Library" in Music. It does the following:
tell application "System Events"
tell process "Music"
set libmenu to menu item "Library" of menu "File" of menu bar 1
click menu item "Export Library…" of menu of libmenu
tell window 1
click button "Save"
tell sheet 1 to click button "Replace"
end tell
end tell
end tell
(So basically it opens the menu and clicks the obvious buttons, saving me a few clicks). However, after upgrading to Ventura (macOS 13.0), this stopped working. The command click button "Save" is failing with:
Can’t get button \"Save\" of window 1 of process \"Music\"."
I tried to say click button 1 or click button 2 instead, but that doesn't work. I then said name of every button and it printed
{missing value, missing value, missing value}
I couldn't find a good manual for AppleScript, but it does look like something changed in Ventura. Any hints will be appreciated!
Thanks,
OK, I replaced click button "Save" with:
repeat with b in (get buttons)
if name of b is "Save" then
click b
end if
end repeat
Interestingly, I can still click the "Replace" button directly without resorting to a loop.
Thanks!
OK! I found a hint for the answer here: Just add a short delay before trying to click on "Save"!
delay 0.5
click button "Save"
Without the delay, the the dialog doesn't seem to be ready and indeed if you do get buttons you will only see the 3 from the top left corner (close, minimize, zoom). With the delay, get buttons returns 6 buttons, including the "Save".

How can I click OK with Apple Script?

I'm trying to select OK with the popup for scaling the screen with Apple script.
Does anyone know what I can add to this script to allow me to click OK?
Apple Script:
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
click radio button "Scaled" of radio group 1 of tab group 1
click radio button 1 of radio group 1 of group 1 of tab group 1
(* Trying to click the ok button here *)
delay 1
click ((pop up buttons of sheet 1 of window 1) whose description is "OK")
end tell
quit application "System Preferences"
Or also rather does anyone know a trusted application that can graphically print out the labels of the UI when I visit them on my screen so I will know what to use when I say: "tell UIName"?
Thanks.
Solution 1: Press Return
Since you're using System Events anyway, and the button in question is selected by default (indicated by being highlighted in blue), just make sure that the preference pane is in focus and use System Events to hit the return key:
tell application "System Events" to keystroke return
It's quick, simple, and saves the hassle of identifying UI elements within the hierarchy. The drawback is that the preference pane has tone in focus, and if it loses focus before it receives the keystroke, the rest of the script will fail.
Solution 2: The One You Asked For
Regarding identification of UI elements within the GUI objects hierarchy, you got the class of the button in question wrong as well as its description.
The "OK" button is referenced like so:
tell application "System Events" to tell process "System Preferences" ¬
to get button "OK" of sheet 1 of window "Built-in Retina Display"
(window 1 is also fine). You can use the whose filter to target it instead, which would be done like this (via System Events and the System Preferences process):
get buttons of sheet 1 of window 1 whose name is "OK"
but all this does is ask AppleScript to search for a button whose name we know, then annoyingly give the result back to us as a list (the list structure can be flattened by requesting the first button of sheet 1...).
However, we do know its name and that there's only one of it, so we can reference it by name directly.
As a side-bar, if you quickly run this command:
get the properties of button "OK" of sheet 1 of window "Built-in Retina Display"
you'll be able to see that its description is merely "button", which isn't what you had hoped. Now run this command:
get the actions of button "OK" of sheet 1 of window "Built-in Retina Display"
which reveals it has one action available, AXPress (this is equivalent to a mouse click).
Therefore, finally, the way to click the button in a more satisfactory way than hitting the return key looks like this:
tell application "System Events" to tell process "System Preferences" ¬
to tell button "OK" of sheet 1 of window "Built-in Retina Display" ¬
to perform action "AXPress"
For exploring the GUI objects on the screen, I occasionally use the Accessibility Inspector that comes with Apple's XCode. It's vaguely useful, although inconveniently annoying with some of the discrepancies between the names by which it references objects and the names by which AppleScript does (they're also really subtle discrepancies, but enough to stop your script from working and send you into a frenzy for weeks).
So, actually, I just explore it programmatically myself in Script Editor.
What I did to solve your issue was to bring up the pane and the dialog box in question, then systematically tell the System Preferences process to get UI elements; then get buttons of UI elements; then get buttons of UI elements of UI elements, and stop when I saw a pair of buttons returned called "OK" and "Cancel". It can be pain-staking, but it'll get you the correct reference.
There are other ways too, but I would be moving beyond the scope of this question.

AppleScript - how do I select a menu item in a pop up button that has no title?

Relatively new to AppleScript here...
I'm trying to create an AppleScript to automate a File/Save Page As... action in Firefox. Specifically, I need to select "Web Page, complete" from the Save As... dialog instead of the default "All Files" selection in the pop up button at the bottom of the dialog box. (I'm using Firefox and this option specifically because I want to save the current html contents after some JavaScript code has run - to parse out values for subsequent processing).
I've been able to hack my way around this problem by selecting the pop up menu (which has no title) by using:
((pop up buttons of window "Save As") whose description is "All Files")
and by sending the key stroke "w" to select "Web Page, complete" in the pop-up menu.
I'm trying to find a more robust way of doing this instead of relying upon the fact that "w" selects the menu item that I want. I tried:
click menu item "Web Page, complete" of
((pop up buttons of window "Save As") whose description is "All Files")
but that didn't work. In looking at Accessibility Inspector, it looks like there is a menu between the pop up button (drop down list) and the menu item but I can't figure out how to refer to it.
Any help would be appreciated. Here's the full script:
tell application "Firefox" to activate
delay 0.25
tell application "System Events"
tell process "Firefox"
set frontmost to true
click menu item "Save Page As…" of menu "File" of menu bar 1
delay 0.25
repeat until window "Save As" exists
delay 0.5
end repeat
click ((pop up buttons of window "Save As") whose description is "All Files")
delay 0.5
-- This didn't work:
click menu item "Web Page, complete" of ((pop up buttons of window "Save As") whose description is "All Files")
-- This works but only because the first entry is "Web Page, complete"
keystroke "w"
keystroke return
delay 0.5
set outputfilename to "foo3.html" as text
keystroke outputfilename
keystroke return
delay 0.5
end tell
end tell
try this
activate application "Firefox"
tell application "System Events"
tell process "Firefox"
set frontmost to true
click menu item "Save Page As…" of menu "File" of menu bar 1
repeat until window "Save As" exists
delay 0.2
end repeat
tell window "Save As"
tell pop up button 1 of group 1
if value is not "Web Page, complete" then
click
delay 0.5
pick menu item "Web Page, complete" of menu 1
end if
end tell
set outputfilename to "foo3.html"
keystroke outputfilename
click button "Save"
end tell
end tell
end tell

Accesing third party menu extras (menulets) via applescript?

Is it possible to access third party menulets via applescript? (Those icons displayed in the top right corner of the global menu bar).
I basically want to know if a certain menu item (that is shown when you click the icon) is enabled or disabled (greyed out).
Any resources on this?
Thanks
Yes, menu items have an "enabled" property. That property is false for "greyed out" menu items. So for example, I show the clock menulet in my main menu bar. If I wanted to know the enabled property of each of its menu items I could do this...
tell application "SystemUIServer" to activate
set theProps to {}
tell application "System Events"
tell process "SystemUIServer"
set menulets to menu bar items of menu bar 1
repeat with aMenu in menulets
if (description of aMenu) is "clock" then
click aMenu -- we have to open it to access the menu items inside it
delay 0.2
set clockMenuItems to menu items of menu 1 of aMenu
repeat with aMenuItem in clockMenuItems
set end of theProps to {title of aMenuItem, enabled of aMenuItem}
end repeat
end if
end repeat
end tell
end tell
return theProps
Note that some of those menulets are not regular menulets. Those you have to treat differently but the concept is the same. You click the menulet then access its menu items and check their enable property.

Resources