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

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.


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"