Click on Commands and Anchor Vocabulary "Dictation"

Hello.
I learned how to enable and disable “Voice Control” using AppleScript



tell application "System Preferences"
	reveal anchor "Dictation" of pane id "com.apple.preference.universalaccess"
	-- activate
end tell
delay 0.5

tell application "System Events"
	tell process "System Preferences"
		click checkbox "Enable Voice Control" of group 1 of window "Accessibility"
	end tell
end tell



However I can’t find the code to click on the buttons (Commands and Vocabulary) that allow access to the module for creating and editing Voice Commands nor to the module that allows to add or edit the Vocabulary.

I would appreciate any help to learn it.

This works for me using macOS Monterey

This opens “Commands…”

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 "Dictation" of pane "com.apple.preference.universalaccess"
	repeat while not (exists of anchor "Dictation" of pane "com.apple.preference.universalaccess")
		delay 0.1
	end repeat
	activate
end tell
tell application "System Events" to tell process "System Preferences"
	repeat while not (exists of button "Commands…" of group 1 of window "Accessibility")
		delay 0.1
	end repeat
	click button "Commands…" of group 1 of window "Accessibility"
end tell

This opens “Vocabulary…”

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 "Dictation" of pane "com.apple.preference.universalaccess"
	repeat while not (exists of anchor "Dictation" of pane "com.apple.preference.universalaccess")
		delay 0.1
	end repeat
	activate
end tell
tell application "System Events" to tell process "System Preferences"
	repeat while not (exists of button "Vocabulary…" of group 1 of window "Accessibility")
		delay 0.1
	end repeat
	click button "Vocabulary…" of group 1 of window "Accessibility"
	repeat while not (exists of button 1 of group 1 of sheet 1 of window "Accessibility")
		delay 0.1
	end repeat
	click button 1 of group 1 of sheet 1 of window "Accessibility"
end tell

Thank you very much, wch1zpink for your answer (fast, complete and didactic).

Very clarifying the use of timeouts through the use of “repeat until…” and “repeat while not” loops and to know what is their proper syntax for their use.

I have tried to analyze why in block 'tell application “System Preferences” ’ the anchor “Dictation” is invoked and then the timeout loop ‘repeat while not (exists of anchor “Dictation”…’.
However, in the next block 'tell application “System Events” ’ the wait loop 'repeat while not (exists of button “Commands…” '. I wonder if the reason is because in the previous block the invocation was already done and in this block it is only necessary to confirm that the button “Command…” is already available.

I also want to take this opportunity to ask what exactly is meant by “group 1 of window”.

To make it work I had to rename the panel and the buttons to their spanish names and… what a joy to see it working perfectly!

Thank you very much from the bottom of my heart.

As you already know, sometimes it is necessary to use a “delay” command between 2 commands. Which basically gives one command the time to finish before the next command executes. The only problem with that is that it is a guessing game as to how much of a delay to use. The delay time that you use may work always or maybe just sometimes. For example, your computer may be low on resources because you have many applications running and your system may be running slow because of that. So you run a script that contains a delay .5 command and you get an error but delay 1 works.

Using the “repeat until…” and “repeat while not” loops with a delay .1, allows the code itself to continuously check every .1 seconds… for example: until the previous action has thoroughly completed before continuing on to the next command or maybe you told an app to quit and then you want to re-launch that app again so you use a repeat while “ABC.app” is still running loop before re-launching “ABC.app”