0

I am trying to use AppleScript to press the switch for activating voice control in macOS Ventura.

The first switch in this image where it toggles voice control:

the first switch in this image where it toggles voice control

I tried the following apple script:

do shell script "open -b com.apple.systempreferences " & ¬
        "/System/Library/PreferencePanes/UniversalAccessPref.prefPane"

    tell application "System Events"
        tell its application process "System Settings"
            repeat until UI element 4 of group 1 of scroll area 1 of group 1 of ¬
                group 2 of splitter group 1 of group 1 of window "Accessibility" exists
                delay 0.1
        end repeat
        click UI element 1 of group 3 of scroll area 1 of group 1 of group 2 ¬
                of splitter group 1 of group 1 of window "Accessibility"
        repeat until checkbox 3 of group 2 of scroll area 1 of group 1 of group ¬
            2 of splitter group 1 of group 1 of window "Voice Control" exists
            delay 0.1
        end repeat
        click button 5 of group 1 of scroll area 1 of window "Voice Control"
        end tell
    end tell

    tell application "System Settings" to quit

Now it does open up the voice control page as shown in the image above. However, it never presses the switch.

I am running this AppleScript in pycharm python with the applescript module (I don't think it really affects the situation much).

HangarRash
  • 7,314
  • 5
  • 5
  • 32

2 Answers2

2

Here is a working script, integrating the above answer.

This version integrates a corrected reference to the Voice Control checkbox, which was provided (above) by Ron Reuter. This works for me under macOS Ventura, as of 2023-01-30.

do shell script "open -b com.apple.systempreferences " & ¬
    "/System/Library/PreferencePanes/UniversalAccessPref.prefPane"

tell application "System Events"
    tell its application process "System Settings"
        repeat until UI element 4 of group 1 of scroll area 1 of group 1 of ¬
            group 2 of splitter group 1 of group 1 of window "Accessibility" exists
            delay 0.1
        end repeat
        click UI element 1 of group 3 of scroll area 1 of group 1 of group 2 ¬
            of splitter group 1 of group 1 of window "Accessibility"
        repeat until checkbox "Voice Control" of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Voice Control" exists
            delay 0.1
        end repeat
        click checkbox "Voice Control" of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Voice Control"
        
    end tell
end tell

tell application "System Settings" to quit
BlakeTNC
  • 941
  • 11
  • 14
  • thanks! it works. how do you get to this point however?—i mean where do you get the info to build this prompt – elbOlita Mar 27 '23 at 19:52
  • @elbOlita, Good question. See the comments section under this answer. https://stackoverflow.com/a/75014746/3142960 – BlakeTNC Mar 31 '23 at 17:07
1

For Ventura, the reference to the Voice Control switch is:

click checkbox "Voice Control" of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Voice Control"
Ron Reuter
  • 1,287
  • 1
  • 8
  • 14
  • I posted a script on this page based on your answer, but for people who know very little about AppleScript, how did you get the info you needed for this line of code? In other words how did you "see" or analyze the hierarchy of components so that you could specify all these correct group and splitter numbers, with correct control types and names, etc? Another user asked me this question (@elbOlita), and I'm curious about it too. – BlakeTNC Mar 28 '23 at 23:04
  • I use the excellent Script Debugger application, which has a built-in explorer that uses System Events to reveal all the UI elements for an app, and then lets you drag snd drop from its list into a script to insert statements like the one in the answer. You can get this info the hard way by drilling down by querying UI elements repeatedly, but Script Debugger saves a lot of time. – Ron Reuter Mar 30 '23 at 15:28