更改默认的文字转语音

时间:2011-06-14 22:59:58

标签: applescript text-to-speech voice

Tell application "System Preferences" 
    set "default voice" to "Agnes" 
end tell 

结果是:

  

无法将“默认语音”设置为“anna”。   不允许访问。

4 个答案:

答案 0 :(得分:6)

您的方法存在两个问题:

  • System Preferences应用程序的字典不包含default voice元素或任何其他元素,用于更改TTS(文本到语音)默认语音(从OS X 10.11开始);事实上,似乎 Apple没有提供更改默认语音的编程方式(甚至不是通过其NSSpeechSynthesizer Cocoa类。)
  • 通过双引号default voice,您尝试为字符串文字分配一个值,该值始终会失败。

注意:此答案的早期版本指向Dropbox位置中名为voice的Bash脚本;此脚本已重命名为voices,修改了其语法,现在已正确发布为开源项目 - 见下文。

不幸的是,从OSX 10.11(El Capitan)开始,没有记录的编程方式来更改默认语音。

可以这样做,但这样做需要在未记录的系统内部,因此未来的兼容性无法保证。

voices是我写的一个CLI就是这样 - 经过验证可以在OSX 10.11上运行到OSX 10.8。

然后,您可以从AppleScript执行以下操作:

do shell script "/path/to/voices -d {voiceName}"

例如,如果您将voices放入/usr/local/bin并希望切换为Agnes作为默认语音,请使用:

do shell script "/usr/local/bin/voices -d Agnes"

如果您碰巧安装了Node.js,可以安装voices /usr/local/bin

npm install voices -g

否则,请按照instructions here

答案 1 :(得分:2)

~/Library/Preferences/com.apple.speech.voice.prefs.plist的更改似乎会立即应用。

d=com.apple.speech.voice.prefs
if [[ $(defaults read $d SelectedVoiceName) = Kathy ]]; then
  defaults write $d SelectedVoiceCreator -int 1835364215
  defaults write $d SelectedVoiceID -int 201
  defaults write $d SelectedVoiceName Alex  
else
  defaults write $d SelectedVoiceCreator -int 1836346163
  defaults write $d SelectedVoiceID -int 2
  defaults write $d SelectedVoiceName Kathy
fi

使用UI脚本的另一个选项:

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

如果系统偏好设置之前未打开,则没有延迟,值为Loading Voices…

答案 2 :(得分:1)

要让它与Yosemite一起使用,您需要将以下两行添加到上面mklement0提供的脚本的底部:

来自mklement0的文件的原始链接:https://dl.dropboxusercontent.com/u/10047483/voice

添加以下两行以重新启动SpeechSynthesisServer,否则您无法使用快捷键立即访问新的默认语音:

killall SpeechSynthesisServer
open /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/SpeechSynthesis.framework/Versions/A/SpeechSynthesisServer.app

答案 3 :(得分:0)

这是有效的:

property currentVoice : "Vicki"

set systemVoices to {"Agnes", "Albert", "Alex", "BadNews", "Bahh", "Bells", "Boing", "Bruce", ¬
 "Bubbles", "Cellos", "Deranged", "Fred", "GoodNews", "Hysterical", "Junior", "Kathy", ¬
 "Organ", "Princess", "Ralph", "Trinoids", "Vicki", "Victoria", "Whisper", "Zarvox"}

    repeat

     activate me

     set theResult to display dialog "Say What?" default answer ¬

      "" buttons {"Quit", "Speak", "Change Voice"} ¬

      default button "Speak" cancel button "Quit"


if button returned of theResult is "Quit" then exit repeat

 else if button returned of theResult is "Change Voice" then

  set currentVoice to item 1 of ¬

(choose from list systemVoices with prompt "Choose new voice.")

 end if

 if text returned of theResult is not "" then

  say text returned of theResult using currentVoice volume 1

 end if

end repeat