The Computer Oracle

Mac OS X - Quickly change voices for Text-to-Speech

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Mac Os X - Quickly Change Voices For Text-To-Speech
00:43 Accepted Answer Score 9
01:42 Answer 2 Score 3
02:33 Answer 3 Score 0
03:40 Thank you

--

Full question
https://superuser.com/questions/468130/m...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#macos #mac #texttospeech

#avk47



ACCEPTED ANSWER

Score 9


I have used FastScripts to assign a shortcut to this script:

try
    set old to the clipboard as record
end try
try
    tell application "System Events" to keystroke "c" using command down
    delay 0.05
    say (the clipboard) using "Kyoko"
end try
try
    set the clipboard to old
end try

You could also create a service in Automator:

There is a bug in 10.7 and 10.8 where the shortcuts for Automator services don't always work until you hover over the services menu from the menu bar. WorkflowServiceRunner can also use over 100% CPU while speaking text.

Another option would be to use UI scripting to change between two voices:

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
        click
        delay 0.1
        if value is "Alex" then
            click menu item "Victoria" of menu 1
        else
            click menu item "Alex" of menu 1
        end if
    end tell
end tell
quit application "System Preferences"

Changing the SelectedVoiceID key in com.apple.speech.voice.prefs.plist also works, but I don't know how to apply the changes immediately.




ANSWER 2

Score 3


Thank you very much Lauryi.

I have extended your UI scripting approach to work properly with german and english voices. The problem is, when your system language is not english, all non system languages are hidden (if not currently selected). You have to select: show more voices.. first to get to the desired language. My code lack a bit of elegance, but works. Here it is (updated):

tell application "System Preferences"
    reveal anchor "TTS" of pane "com.apple.preference.speech"
end tell
set tom to 0
tell application "System Events" to tell process "System Preferences"
    tell pop up button 1 of tab group 1 of window 1
        click
        delay 0.2 -- without this the value was sometimes "Loading Voices…"

        if value is "Tom" then
            click menu item "Anna" of menu 1
        else
            click menu item "Mehr Stimmen anzeigen" of menu 1 -- show up all available voice
            set tom to 1
        end if
    end tell
end tell
if tom is 1 then
    delay 0.5
    tell application "System Events" to tell process "System Preferences"
        tell pop up button 1 of tab group 1 of window 1
            click
            delay 0.2 -- without this the value was sometimes "Loading Voices…"
            click menu item "Tom" of menu 1
        end tell
    end tell
end if
quit application "System Preferences"



ANSWER 3

Score 0


Direct changes to ~/Library/Preferences/com.apple.speech.voice.prefs.plist are not really needed if you get the bash-script Voices which really adds all the command-line functionality you need.

An Apple Script to change the standard voice to Alex using Voices would simply look like this:

on run
    do shell script "voices -d Alex"
end run

I do prefer the terminal, and instead of testing the polyglot menu-bar intrusion, I made this (admittedly simple-stupid) shell script (which uses voices) for my language-switching needs. With it, All I do to change the default language is to pop into the terminal to type speak swedish or speak french. This fits excellently into my workflow. I hope you can find a solution that fits yours.

# Choose a voice in one of some selected languages
# Use "voices" from https://github.com/mklement0/voices#manual-installation

if [[ $1 = "" ]]
then
    echo "ERROR. No language specified. Type a language as in 'speak hebrew'"
fi
if [[ $1 = "swedish" || $1 = "Swedish" ]]
then
    voices -d Klara
fi
if [[ $1 = "english" || $1 = "English" ]]
then
    voices -d Daniel
fi
if [[ $1 = "american" || $1 = "American" ]]
then
    voices -d Alex
fi
if [[ $1 = "french" || $1 = "French" ]]
then
    voices -d Aurelie
fi
if [[ $1 = "spanish" || $1 = "Spanish" ]]
then
    voices -d Jorge
fi

I save it to my scripts as "speak.command", chmod it +x, and add the appropriate alias to my .bash_profile to evoke it by typing speak.