Reputation: 3092
How do I get a list of installed voices with AppleScript? I see that sometime ago was used: "tell application voices" or seeing what files were in the directory "/System/Library/Speech/Voices/" but it seems that these methods don't work anymore.
Upvotes: 1
Views: 2179
Reputation: 6932
You can use the shell script say and -v option to get the list.
do shell script "say -v?"
-v voice, --voice=voice Specify the voice to be used. Default is the voice selected in System Preferences. To obtain a list of voices installed in the system, specify '?' as the voice name.
On my system I will get a long list like:
Deranged en_US # I need to go on a really long vacation.
Fred en_US # I sure like being inside this fancy computer
Good News en_US # Congratulations you just won the sweepstakes and you don't have to pay income tax again.
Hysterical en_US # Please stop tickling me!
Jorge es_ES # Hola, me llamo Jorge y soy una voz española.
To pull just the names I will use the spaces after the names as a separator to split the names and junk
set the_name to do shell script "say -v? | awk -F\"\\ \\ \" '{print $1}'"
So here I pipe the result from the say command to awk.
The -F fs option defines the input field separator to be the regular expression fs. So this is where I use the spaces to separate the names from the junk.
I only need to give a double space and not the whole amount of space between the names and the junk. I also have to escape the spaces with a backslash each.
And because we are in Applescript we need to actually escape the escapes to pass them onto the the shell. and of course escape the extra quotes.
\"\\ \\ \"
The awk {print $1} will print out all the fields in field 1 ($1)
which gives me:
Deranged
Fred
Good News
Hysterical
Jorge
Upvotes: 6