【问题标题】:Changing the default text-to-speech voice更改默认文本到语音的语音
【发布时间】:2021-06-23 05:35:07
【问题描述】:
Tell application "System Preferences" 
    set "default voice" to "Agnes" 
end tell 

结果是:

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

【问题讨论】:

    标签: applescript text-to-speech voice


    【解决方案1】:

    你的方法有两个问题:

    • 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 with

    npm install voices -g
    

    否则,请关注instructions here

    【讨论】:

    • 这很好用。谢谢你的剧本。现在,我终于可以轻松地让我的 Mac 在需要时读出德国和美国的文章了。
    • @pattulus:我很高兴听到这个消息;我的荣幸。
    • @pattulus:原始脚本不再完全适用于 OSX 10.10 - 我已在 github.com/mklement0/voices 上以 voices 的名义发布了一个固定和修订版本。
    • 感谢您为我着想。我真的很怀念这个剧本。
    【解决方案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…

    【讨论】:

    • GUI 脚本编写方法很有趣,但如果System Preferences 当前未运行,它(不可避免地)在视觉上会造成干扰,并且可能由于时间问题而失败。写信给.../com.apple.speech.voice.prefs.plist 也是我的voice 实用程序(链接到我的答案)所做的——挑战是找到所有legacy 声音的SelectedVoiceCreator/SelectVoiceID 值。 voice 包含预编译的此信息 - 现代语音允许按需提取此信息。背景:evernote.com/shard/s69/sh/5f95cd0e-9a75-4739-9c7d-5462be20aa09/…
    • 遗憾的是,至少从 OSX 10.10 开始,对 ~/Library/Preferences/com.apple.speech.voice.prefs.plist 的更改不再立即生效(say CLI 除外)。 eplt's answer 包含一个解决方法。
    【解决方案3】:

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

    来自 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
    

    【讨论】:

    • 谢谢你;我已将它合并到我在github.com/mklement0/voices 上发布的voices CLI 中。 (事实证明,我之前只运行 pkill com.apple.speech.speechsynthesisd 的建议并没有完全奏效 - 语音变化本身就被拾起,但没有兑现自定义语速 - 除了正常工作之外,您的修复速度也更快。)
    【解决方案4】:

    这是有效的:

    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
    

    【讨论】:

    • 虽然这对于交互式播放语音很方便(它会提示输入文本,并可以从语音列表中进行选择),但它不会更改系统的默认语音。 (此外,声音列表是硬编码的;“BadNews”应该是“Bad News”。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 2023-03-03
    • 2021-09-06
    • 2015-06-16
    • 2011-06-08
    • 1970-01-01
    • 2017-02-17
    相关资源
    最近更新 更多