AppleScript to make the display black and white for monteray - applescript

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

Related

UI automation, appleScript Keyboard shortcuts for non-menued items Preview?

I am trying to create a keyboard shortcut action for Preview, namely Draw and Sketch. However, they are are the NON-menued items, which means can't get it done in System Preference I see someone done it from inspiration, it is possible, but when I try to follow alone, here is my code so far and please help me complete this. here are the action.
Here is the error I am facing now
after some researches, does the UI/accessibility inspector help?
activate application "Preview"
delay 0.4
set the menuItem to "Draw"
tell application "System Events"
try
tell application process "Preview" to click radio button menuItem of radio group 1 of splitter group 1 of window 1
on error
try
tell application process "Preview" to click radio button menuItem of radio group 1 of window 1
on error errorM
display dialog errorM
end try
end try
end tell
ideally, trying to make it to work when all bars are hidden,
but if not possible. can we make it to work vwith mark up bar is shown. as below.
This work for me using the latest version of macOS Mojave
tell application "Preview" to activate
repeat while application "Preview" is not running
delay 0.2
end repeat
tell application "System Events"
try
click menu item "Show Markup Toolbar" of menu 1 of menu bar item "View" of menu bar 1 of application process "Preview"
end try
delay 0.5
try
click menu item "Show Toolbar" of menu 1 of menu bar item "View" of menu bar 1 of application process "Preview"
end try
delay 0.5
repeat while not (exists of toolbar 1 of window 1 of application process "Preview")
delay 0.2
end repeat
set description2 to a reference to every checkbox of toolbar 1 of window 1 of application process "Preview"
set theCheckboxes to description of description2
if item 1 of theCheckboxes is "Draw" then
set checkBoxDraw to 1
else
set checkBoxDraw to 2
end if
if item 1 of theCheckboxes is "Sketch" then
set checkBoxSketch to 1
else
set checkBoxSketch to 2
end if
delay 1
-- Below, insert either checkBoxSketch for "Sketch" or checkBoxDraw for "Draw"
click checkbox checkBoxDraw of toolbar 1 of window 1 of application process "Preview"
end tell
-- without these next following lines, the toolbar "Draw" or "Sketch" do not appear to be selected
tell application "Preview" to tell window 1
set visible to false
set visible to true
end tell
delay 3

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.

Speed up AppleScript UI scripting?

I'm using NetShade as a proxy service and thought I could try to automate the switching between the different proxies as a nice start for my first AppleScript script.
The NetShade-app has no AppleScript support, so I have to use UI scripting. After a few tries (and some posts here) I managed to have a script, that switches the proxies via the menu bar item (here is a picture of it, since I can't post it inline due to reputation limit).
Unfortunately my code is extremely slow (≈6sec), which makes it kind of impractical as a script. The first menu opens immediately, but the selection of the sub-menu and the proxy server takes several seconds.
I'm using the following code:
set theProxy to "Netshade US 4"
tell application "System Events" to tell process "NetShade"
tell menu bar item 1 of menu bar 2
click
tell menu item "NetShade Proxy" of menu 1
click
tell menu item theProxy of menu 1
click
end tell
end tell
end tell
end tell
I already tried to add ignoring application responses, like suggested in a different thread (link), but that didn't help.
So finally my questions:
Is there a way to speed the process up? Maybe even a way to do all this in the background, without showing the menu items?
P.S.: I'm running OS X 10.9.1
Summary of the fix
To remove delay you need to do two things:
(I) Identify the click which is causing the delay and enclose only that line in the ignoring application responses block as shown below. In my case, it was click bt after which the execution was going into a wait mode for 5 to 6 seconds.
ignoring application responses
click bt
end ignoring
(II) I then also had to kill System Events to and start it again using the following commands.
do shell script "killall System\\ Events"
delay 0.1
-- Rest of the code to click stuff or send keycodes
This resolved the delay issue.
Details
I was having the same problem where I created a script to connect/disconnect my bluetooth headset through AppleScript. The script is given below.
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
The script was working fine but had a problem where it would wait for 5 to 6 seconds after executing "click bt" above. I modified the code as follows and it is working absolutely fine now without any delay.
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell

Applescript automation flipping checkbox not always working

I have the following applescript that is acting strangely. If the file sharing checkbox is unchecked it will check it everytime. If the file sharing checkbox is already checked it will sometimes uncheck it. then when it comes to the if statement that is to determine if the current state of the file sharing checkbox(this is to take place after the checking action) it never reads it as checked.
here is the thought proccess:
open system prefs (works)
show sharing pane (works)
click the file sharing check box, row 3. (sort of works. checks if unchecked. does not uncheck)
get current state of file sharing box, generate appropriate message(does not work)
quit system prefs(works)
Display message about what action was taken(works)
Apple Script
tell application "System Preferences"
activate
reveal (pane id "com.apple.preferences.sharing")
end tell
tell application "System Events"
tell process "System Preferences"
try
click checkbox of row 3 of table 1 of scroll area of group 1 of window "Sharing"
delay 2
if checkbox of row 3 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then
set response to "File Sharing turned on"
else
set response to "File Sharing turned off"
end if
tell application "System Preferences" to quit
activate (display dialog "Flipped")
on error
activate
display dialog "something went wrong in automation but you are in the right menu..."
return false
end try
end tell
end tell
Most of the time, the problem is that the window is not yet fully shown, so you should ask the UI element whether it is already available:
== I´ve edited the script below to reflect changes based on the comments.
tell application "System Preferences"
activate
reveal (pane id "com.apple.preferences.sharing")
end tell
tell application "System Events" to tell table 1 of scroll area of group 1 of window 1 of process "System Preferences"
tell (1st row whose value of static text 1 is "File Sharing")
set sharingStatus to value of checkbox 1 as boolean
if sharingStatus is true then
click checkbox 1
my notify("File Sharing is now turned off")
else
click checkbox 1
my notify("File Sharing is now turned on")
end if
end tell
end tell
on notify(notification)
display dialog notification
end notify

In Applescript, how can I find out if a menu item is selected/focused?

I have a script for OS X 10.5 that focuses the Search box in the Help menu of any application. I have it on a key combination and, much like Spotlight, I want it to toggle when I run the script. So, I want to detect if the search box is already focused for typing, and if so, type Esc instead of clicking the Help menu.
Here is the script as it stands now:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
click helpMenuItem
end tell
end tell
And I'm thinking of something like this:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
set searchBox to menu item 1 of menu of helpMenuItem
if (searchBox's focused) = true then
key code 53 -- type esc
else
click helpMenuItem
end if
end tell
end tell
... but I get this error:
Can’t get focused of {menu item 1 of menu "Help" of menu bar item "Help" of menu bar 1 of application process "Script Editor" of application "System Events"}.
So is there a way I can get my script to detect whether the search box is already focused?
I solved my problem by working around it. I still don't know how to check if a menu item is selected though, so I will leave this topic open.
You need to use attribute AXMenuItemMarkChar.
Example:
tell application "System Events"
tell process "Cisco Jabber"
set X to (value of attribute "AXMenuItemMarkChar" of menu item "Available" of menu "Status" of menu item "Status" of menu "File" of menu bar item "File" of menu bar 1) is "✓" -- check if Status is "Availible"
end tell
end tell
If the menu item is checked, the return value is ✓, otherwise it is missing value.
Note: This test only works if the application whose menus are being inspected is currently frontmost.
The built in key shortcut Cmd-? (Cmd-Shift-/) already behaves like this. It moves key focus to the help menu's search field if it is not already focused, and otherwise dismisses the menu.
Using /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app you can use the built-in accessibility system to look at properties of the UI element under the mouse. Take special note of the cmd-F7 action to lock focus on an element and the Refresh button. Sadly the element and property names don't directly match those in the script suite, but you can look at the dictionary for System Events or usually guess the right terminology.
Using this you can determine two things. First, the focused property isn't on the menu item, but rather there is a text field within the menu item that is focused. Second, the menu item has a selected property.
With this, I came up with:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
-- Use reference form to avoid building intermediate object specifiers, which Accessibility apparently isn't good at resolving after the fact.
set searchBox to a reference to menu item 1 of menu of helpMenuItem
set searchField to a reference to text field 1 of searchBox
if searchField's focused is true then
key code 53 -- type esc
else
click helpMenuItem
end if
end tell
end tell
Though this still doesn't work. The key event isn't firing as far as I can tell, so something may still be hinky with the focused property on the text field.
Anyway, your click again solution seems much easier.
I just came across the need to do this myself for some file processing in Illustrator.
Here is what I came up with:
tell application "Adobe Illustrator"
activate
tell application "System Events"
tell process "Illustrator"
set frontmost to true
set activeMenuItem to enabled of menu item "Unlock All" of menu "Object" of menu bar item "Object" of menu bar 1
if activeMenuItem is true then
tell me to beep 3
else
tell me to beep 2
end if
end tell
end tell
end tell
Done.
This worked with no problem and could be used to iterate a file. I'll probably have to do this many more times in my future automation.
Good luck!
This worked for me to toggle between two menu items, based on which one is selected, using the "selected" property:
tell application "System Preferences"
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 2 of tab group 1 of window 1
click
delay 0.2
set appControl to menu item "App Controls" of menu 1
set fKeys to menu item "F1, F2, etc. Keys" of menu 1
if selected of appControl is true then
click fKeys
else
click appControl
end if
end tell
end tell

Resources