如何使用applescript切换新听写工具的语言设置(10.8)

如何使用applescript切换新听写工具的语言设置(10.8),applescript,Applescript,我真的很喜欢MacOSX Mountain Lion的新口述功能。我用两种语言使用它;英语(美国)和法语 每次我需要切换语言时,我必须进入系统首选项、听写和语音,然后选择语言 现在,我想使用Applescript自动完成这项工作,不幸的是,由于它太新了,我无法获得听写模块的正确字符串 快速示例(这只是一个开始): 对于xxxx,我尝试了“听写和演讲”,这是一个毫无根据的猜测 关于我如何获得“听写和演讲”的确切字符串,有什么想法吗 提前感谢, François要获取窗格id:进入系统首选项,选择一

我真的很喜欢MacOSX Mountain Lion的新口述功能。我用两种语言使用它;英语(美国)和法语

每次我需要切换语言时,我必须进入系统首选项、听写和语音,然后选择语言

现在,我想使用Applescript自动完成这项工作,不幸的是,由于它太新了,我无法获得听写模块的正确字符串

快速示例(这只是一个开始):

对于xxxx,我尝试了“听写和演讲”,这是一个毫无根据的猜测

关于我如何获得“听写和演讲”的确切字符串,有什么想法吗

提前感谢,


François

要获取窗格id:进入系统首选项,选择一个窗格,在编辑器中运行此脚本

tell application "System Preferences" to get id of current pane

结果就是精确的字符串。

您可以编辑存储设置的属性列表,然后重新打开DictationIM过程:

#!/bin/bash

k="com.apple.speech.recognition.AppleSpeechRecognition.prefs DictationIMLocaleIdentifier"
if [[ "$(defaults read $k)" == en-US ]]; then
  defaults write $k fr-FR
  defaults write com.apple.assistant "Session Language" fr-FR
else
  defaults write $k en-US 
  defaults write com.apple.assistant "Session Language" en-US
fi
killall -HUP DictationIM
或使用UI脚本:

delay 0.3 -- time to release modifier keys if the script is run with a shortcut
tell application "System Preferences"
    reveal anchor "Dictation" 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
        if value is "English (United States)" then
            click menu item "French" of menu 1
        else
            click menu item "English (United States)" of menu 1
        end if
    end tell
end tell
quit application "System Preferences"
很酷的东西

如果您安装了“Notifications Scripting(),那么您甚至可以安装一个通知来通知您有关该语言的信息。 通过快速脚本的帮助,您可以为该脚本指定键盘快捷键

-- Switch the language of Mountain Lion's dictation
-- Here, we just toggle between English and German
-- Needs 'Notifications Scripting' ( http://www.cooperative-fruitiere.com/notifications/index_en.html )

delay 0.3 -- time to release modifier keys if the script is run with a shortcut
tell application "System Preferences"
    reveal anchor "Dictation" 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
        if value is "English (United States)" then
            set language to "German (Germany)"

        else
            set language to "English (United States)"
        end if
        click menu item language of menu 1
    end tell
end tell

quit application "System Preferences"

tell application "Notifications Scripting"
    set event handlers script path to (path to me)
    -- The user info parameter is a record. The supported data types are text, integer, real, boolean, date, alias, file and POSIX file.
    set dict to {theName:"Notifications Scripting", theVersion:"1.0", theScript:event handlers script path}
    display notification "Dictation Language" subtitle "Switched to:" message language
end tell

using terms from application "Notifications Scripting"
    -- This handler is called when a notification was delivered.
    on notification delivered title aTitle subtitle aSubTitle message aMessage actual delivery date aDeliveryDate user info aDict
    end notification delivered
end using terms from

除非您输入
delay
命令,否则提供的脚本无法工作。此外,它需要是
应用程序流程
,而不仅仅是
流程
。我将只发布工作代码,而不是解释在哪里进行更改。[请注意,这是在macOS Catalina上测试的。如果苹果在未来版本中更改系统首选项GUI,此代码可能无法工作。]

tell application "System Preferences"
    reveal anchor "Dictation" of pane "com.apple.preference.speech"
end tell
delay 1
tell application "System Events" to tell application process "System Preferences"
    tell pop up button 1 of tab group 1 of window 1
        click
        if value is "English (United States)" then
            click menu item "Hungarian (Hungary)" of menu 1
        else
            click menu item "English (United States)" of menu 1
        end if

    end tell
end tell
quit application "System Preferences"

我希望能够通过脚本将德语转换成英语(英国),反之亦然。不幸的是,我对AppleScript还不太熟悉,因此没有太多经验。一旦脚本运行顺利,您是否介意发布您的结果?提前谢谢!哇!很好,非常感谢,很抱歉我的回复被耽搁了!
tell application "System Preferences"
    reveal anchor "Dictation" of pane "com.apple.preference.speech"
end tell
delay 1
tell application "System Events" to tell application process "System Preferences"
    tell pop up button 1 of tab group 1 of window 1
        click
        if value is "English (United States)" then
            click menu item "Hungarian (Hungary)" of menu 1
        else
            click menu item "English (United States)" of menu 1
        end if

    end tell
end tell
quit application "System Preferences"