2

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

每次我需要切换语言时,我都必须进入系统偏好,听写和语音,然后选择语言。

现在,我想使用 Applescript 自动执行此操作,不幸的是,由于它太新,我无法获得正确的听写模块字符串。

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

tell application "System Preferences"
   activate
   set the current pane to pane id "com.apple.preference.xxxxxx"
end tell

对于 xxxx,我尝试了“Dictation&Speech”的疯狂猜测,但没有成功。

关于如何获得“听写和语音”的确切字符串的任何想法?

预先感谢,

弗朗索瓦

4

5 回答 5

3

您可以编辑存储设置的属性列表并重新打开 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"
于 2012-07-30T11:01:29.007 回答
3

要获取窗格的 ID:进入系统偏好设置,选择一个窗格,在编辑器中运行此脚本。

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

结果是确切的字符串。

于 2012-07-27T04:09:51.553 回答
0

很酷的东西!

如果您安装了“通知脚本”(http://www.cooperative-fruitiere.com/notifications/index_en.html),那么您甚至可以收到通知,通知您有关该语言的信息。在 FastScripts 的帮助下,您可以为该脚本分配键盘快捷键。

-- 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
于 2013-01-06T13:56:20.937 回答
0

delay除非您输入命令,否则提供的脚本不起作用。此外,它需要application process,而不是简单地process。我不会解释在哪里进行更改,而是发布工作代码。[请注意,这是在 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"
于 2020-02-14T06:12:36.090 回答
0

我现在使用 Alfred 来触发这个 bash 脚本。我为它分配了一个热键,并为我说的语言复制了它,所以我为我的三种语言提供了三种不同的键盘快捷键。它使用对上述脚本稍作修改的脚本(使用不同的pref,不再需要delays,并立即触发听写。)

k="com.apple.speech.recognition.AppleSpeechRecognition.prefs DictationIMNetworkBasedLocaleIdentifier"

if [ "$(defaults read $k)" != "de_DE" ]
then
    defaults write $k "de_DE"
    defaults write com.apple.assistant "Session Language" "de_DE"

    killall -HUP DictationIM

-- Somehow I need to do twice the keyboard shortcut to start the dictation. This is the first one.
osascript -e 'tell app "System Events" to key code {63,63}'

fi


-- second one
osascript -e 'tell app "System Events" to key code {63,63}'
于 2022-01-02T14:01:26.637 回答