Enable Voice Control. AppleScript - macos

I try to create an Apple Script to Enable Voice Control in a Mac.
This is in: System Preferences > Accessibility > Voice Control > Enable Voice Control
I think I get close. But I do not know how to call the left menu "Voice control"
This is what I have tried and do not work:
tell application "System Events"
click checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility" of application process "System Preferences" of application "System Events"
end tell

You asked in the comments if there was another way to get the solution. Here's a way to revise your code. First, I will explain the solution, step by step.
Update: This if statement, as pointed out in user3439894's very helpful comments below, is actually necessary, for several reasons. It should always be included.
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
This is done so the script will not fail if it is running. Note that the user also pointed out that killall should be used to ensure that the process completely terminates.
It allows for predictability in the behavior of the application.
user3439894 also pointed out that I should add this code block, in case there is a timing issue, resulting in the application trying to reopen while it is in the process of closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
Next, in this snippet, you click the Accessibility button in System Preferences
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2 -- while loads
Next, you select the Voice Control row in Settings.
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
And finally, click the checkbox. As I explained in the comments, groups are used to organize elements.
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
Full (updated) code:
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
activate application "System Preferences"
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2.0 -- delay while page loads
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
Corrections I made to your code:
You cannot reference elements from windows that are not already opened in an app (like you did with the checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility"), so first I opened the Accessibility window (click a button) -> Voice Control Window (select the row), and then found/clicked the checkbox.
Then, I looked at the hierarchy of the elements to figure out where the checkbox is in that Voice Control Window. I found out that it is inside group 1 of window 1 (Voice Control Window), so I clicked it.
I also added delays (the app needs time to load after every click/select).
Also, here is how I figured out where the elements are.
If you don't already use it, there is this built-in app called Accessibility Inspector to help you locate elements.
You can also use get in applescript. For example,
tell application "System Events"
get buttons of scroll area 1 of window 1 of process "System Preferences"
end tell
would return:
... button "Accessibility" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", button "Screen Time" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events",...
Another example:
tell application "System Events"
get UI elements of window 1 of process "System Preferences"
end tell
and here's some of the output
...scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", toolbar 1 of window "System Preferences" of application process "System Preferences" of application "System Events..."
A very helpful tool.

Related

Applescript to turn mirroring on and select iPad

I recently updated my mac to Monterey and my old applescript is not working anymore and I get this error: 'System Events got an error: Can’t get pop up button 1 of window 1 of process "System Preferences". Invalid index.'
I honestly have no idea which part should I change. Thank you in advance.
tell application "System Preferences"
set current pane to pane "com.apple.preference.displays"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 1 of window 1
click menu item 1 of menu 1 of pop up button 1 of window 1
end tell
end tell
tell application "System Preferences"
delay 10
quit
end tell
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
activate
end tell
tell application "System Events"
tell application process "System Preferences"
click pop up button "Add Display" of window "Displays"
click menu item 2 of menu 1 of pop up button "Add Display" of window "Displays"
end tell
end tell
tell application "System Preferences"
delay 5
quit
end tell

Show Bluetooth / Volume in menu bar Apple Script

I have a working script that I would like to see if there is a way to hide or simply have the script run in the background. The current working script physically activates the pane thus the user is seeing it happen in front of them.
tell application "System Preferences"
activate
set the current pane to pane "Bluetooth"
delay 1
end tell
tell application "System Events"
tell process "System Preferences"
set toggleBluetooth to the checkbox "Show Bluetooth in menu bar" of the window "Bluetooth"
click toggleBluetooth
end tell
end tell
I've also found a way to turn on certain things instantly and was wondering if there was a way to take this and make it work with the bluetooth and volume.
tell application "Finder"
tell Finder preferences
set desktop shows hard disks to true
end tell
end tell
This following AppleScript code can be used to toggle the Bluetooth menu bar item on and off, without bringing System Preferences to the front.
if application "System Preferences" is running then
do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
delay 0.1
end repeat
end if
tell application "System Preferences"
reveal anchor "Main" of pane id "com.apple.preferences.Bluetooth"
end tell
tell application "System Events"
repeat until checkbox "Show Bluetooth in menu bar" of window "Bluetooth" of application process "System Preferences" exists
delay 0.1
end repeat
click checkbox "Show Bluetooth in menu bar" of window "Bluetooth" of application process "System Preferences"
end tell
tell application "System Preferences" to quit
And this will do the exact same thing for the volume icon in the menu bar
if application "System Preferences" is running then
do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
delay 0.1
end repeat
end if
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
tell application "System Events"
repeat until checkbox "Show volume in menu bar" of window "Sound" of application process "System Preferences" exists
delay 0.1
end repeat
click checkbox "Show volume in menu bar" of window "Sound" of application process "System Preferences"
end tell
tell application "System Preferences" to quit

How to toggle "reduce motion" preference with AppleScript?

I'd like to be able to toggle the macOS "reduce motion" preference (system preferences > accessibility > display > display > reduce motion) via the touchbar analogous to this article about toggling dark mode Does anyone have an idea on how to toggle it with AppleScript? I googled around a bit, but came up empty handed.
This AppleScript code works for me using the latest version of macOS Catalina
if application "System Preferences" is running then ¬
do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
delay 0.1
end repeat
tell application "System Preferences"
reveal anchor "Seeing_Display" of ¬
pane id "com.apple.preference.universalaccess"
end tell
tell application "System Events"
repeat until exists of checkbox "Reduce motion" of tab group 1 of ¬
group 1 of window "Accessibility" of application process "System Preferences"
delay 0.1
end repeat
click checkbox "Reduce motion" of tab group 1 of ¬
group 1 of window "Accessibility" of application process "System Preferences"
end tell
tell application "System Preferences" to quit

AppleScript: How to identify checkbox location in System Preferences?

I want to issue a keyboard command to toggle the checkbox "Change picture every 30 minutes" in OS X for Desktop 1 (my main monitor desktop). I have multiple monitors so it may be necessary to identify which particular preference pane to manipulate.
I have set up the start of an AppleScript, but I'm at a loss how to formulate how to identify this particular check box:
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Desktop & Screen Saver" of menu "View" of menu bar 1
delay 2
tell window "Desktop & Screen Saver"
click checkbox 1 of …
It's in tab group 1. You can recognize this with the help of the Xcode tool Accessibility Inspector (Xcode > Open Developer Tool > Accessibility Inspector in the menu bar or Dock).
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.desktopscreeneffect"
end tell
delay 2
tell application "System Events" to tell process "System Preferences"
click checkbox "Change picture:" of tab group 1 of window "Desktop & Screen Saver"
end tell

Use AppleScript to change System Preferences without being visible?

I would like to change settings in System Preferences without the user seeing things happen.
If I have a script that starts like:
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
end tell
the System Preference window will be shown to the user.
I'd like to know if there is a way to do it in the background somehow, or at least keep the window minimized.
(Example script can be found in this question.)
You can just remove the activate command. System Events can perform actions in hidden windows.
tell application "System Preferences"
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events" to tell process "System Preferences"
click checkbox 1 of tab group 1 of window 1
end tell
quit application "System Preferences"
If you open menus, they will be visible though.
tell application "System Preferences"
reveal anchor "TTS" of pane "com.apple.preference.speech"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 1 of tab group 1 of window 1
delay 0.1
click
if value is "Alex" then
click menu item "Kathy" of menu 1
else
click menu item "Alex" of menu 1
end if
end tell
end tell
quit application "System Preferences"
The brightness of displays can also be changed with brightness.c.
You might be able to do what you want with with the command line app defaults, there are some other command line apps that can manipulate other system stuff also like, pmset
Some things can be set using scripting additions also, for example the systems volumn can be set using the standard additions, you also you may be able to find other scripting additions to add more stuff.

Resources