【发布时间】:2021-06-23 05:35:07
【问题描述】:
Tell application "System Preferences"
set "default voice" to "Agnes"
end tell
结果是:
无法将“默认语音”设置为“安娜”。 不允许访问。
【问题讨论】:
标签: applescript text-to-speech voice
Tell application "System Preferences"
set "default voice" to "Agnes"
end tell
结果是:
无法将“默认语音”设置为“安娜”。 不允许访问。
【问题讨论】:
标签: applescript text-to-speech voice
你的方法有两个问题:
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。
【讨论】:
voices 的名义发布了一个固定和修订版本。
对~/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…。
【讨论】:
System Preferences 当前未运行,它(不可避免地)在视觉上会造成干扰,并且可能由于时间问题而失败。写信给.../com.apple.speech.voice.prefs.plist 也是我的voice 实用程序(链接到我的答案)所做的——挑战是找到所有legacy 声音的SelectedVoiceCreator/SelectVoiceID 值。 voice 包含预编译的此信息 - 现代语音允许按需提取此信息。背景:evernote.com/shard/s69/sh/5f95cd0e-9a75-4739-9c7d-5462be20aa09/…
~/Library/Preferences/com.apple.speech.voice.prefs.plist 的更改不再立即生效(say CLI 除外)。 eplt's answer 包含一个解决方法。
要让它与 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
【讨论】:
voices CLI 中。 (事实证明,我之前只运行 pkill com.apple.speech.speechsynthesisd 的建议并没有完全奏效 - 语音变化本身就被拾起,但没有兑现自定义语速 - 除了正常工作之外,您的修复速度也更快。)
这是有效的:
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
【讨论】: