如何使用apple脚本在Mac右上方菜单中选择特定语言(以英语为例)?我的代码真的很慢。大概需要5秒。你有快速而聪明的想法吗?)
tell application "System Events" to ¬
tell application process "TextInputMenuAgent" to ¬
tell menu bar item 1 of menu bar 2
click
click menu item 1 of menu 1
end tell
您可以使用用于选择键盘布局的标准键盘快捷键。
默认是Ctrl-Space(前一个布局)和Alt-Ctrl-Space(下一个布局),尽管它是绕行的,所以如果你只有两种语言,任何一种都可以简单地切换布局。
可以这样编写脚本:
tell application "System Events"
key code 49 using control down
end tell
请注意,这个快捷方式可以被指定的用户在系统参数设置-键盘-快捷方式-输入源中禁用(或更改),尽管如果你是唯一的用户,这不会有问题。
更多的想法可能会发现在改变OSX键盘布局("输入源")通过终端或AppleScript编程?
示例AppleScript下面所示的代码在脚本编辑器中进行了测试作为AutomatormacOS Catalina下的Service/Quick ActionandmacOS Big Sur与语言&地区System Preferences中的设置set toEnglish (US) - Primary并为我工作没有问题1。
- 1在System Preferences中假定必要和适当的设置比;安全,隐私比;隐私strong>
示例AppleScript<代码/em>:
ignoring application responses
tell application "System Events" to ¬
click menu bar item 1 of ¬
menu bar 2 of ¬
application process "TextInputMenuAgent"
end ignoring
delay 0.1
do shell script "killall 'System Events'"
delay 0.2
tell application "System Events"
launch
click menu item 1 of ¬
menu 1 of ¬
menu bar item 1 of ¬
menu bar 2 of ¬
application process "TextInputMenuAgent"
end tell
指出:
第二个tell application "System Events"
块:
改变:
click menu item 1 of ¬
:
菜单项的实际名称,例如click menu item "U.S." of ¬
或其他实际名称。
您也可以更改数字,例如1
为任何数字是合适的。
经过测试,这对我来说没有任何问题,而且,如果没有内置的十分之三秒的综合延迟,这是即时的。
如果示例AppleScript上面显示的代码不适合您,然后使用使用:单击方法显示在我的回答AppleScript -不能摆脱延迟后点击或者对于更健壮的解决方案,也使用cliclick
,一个第三方命令行实用程序,下面的示例AppleScriptcode可用于您试图单击的多个直接菜单项。
例子em>AppleScript<代码/em>:
-- # This script works on menu bar items in menu bar 2.
-- # Set the name of the application process and the
-- # name of the target menu item. It will use cliclick
-- # a third-party command line utility to perform the
-- # clicks as a workaround to the 5 second delay bug.
-- #
-- # Note that this requires the target menu item to have
-- # a name that System Events can retrieve properties for.
-- # It is from these properties it position and size is
-- # ascertained in order to calculate where to click it.
-- # User set properties.
property appProcessName : "TextInputMenuAgent"
property menuItemName : "U.S."
property cliclick : "/usr/local/bin/cliclick"
-- # The code below is tokenized and
-- # should not need to modified.
property xPos : missing value
property yPos : missing value
tell application "System Events" to ¬
set thePositionSizeList to ¬
the {position, size} of ¬
menu bar item 1 of ¬
menu bar 2 of ¬
application process ¬
appProcessName
my setXposYposFrom(thePositionSizeList)
my clickAtXposYpos()
tell application "System Events"
set theTargetMenuItemProperties to ¬
properties of ¬
first menu item of ¬
menu 1 of ¬
menu bar item 1 of ¬
menu bar 2 of ¬
application process ¬
appProcessName whose ¬
name is menuItemName
set thePositionSizeList to ¬
the {position, size} of ¬
theTargetMenuItemProperties
end tell
my setXposYposFrom(thePositionSizeList)
my clickAtXposYpos()
-- ## Handlers ##
to setXposYposFrom(thisList)
set xPos to ¬
(item 1 of item 1 of thisList) + ¬
(item 1 of item 2 of thisList) / 2 ¬
as integer
set yPos to ¬
(item 2 of item 1 of thisList) + ¬
(item 2 of item 2 of thisList) / 2 ¬
as integer
end setXposYposFrom
to clickAtXposYpos()
set shellCMD to {¬
cliclick, " -r c:", ¬
xPos, ",", yPos} as string
do shell script shellCMD
end clickAtXposYpos
<子>注意:示例AppleScriptcode就是这样,并且sans任何包含的错误处理不包含任何额外的错误处理可能是适当的。用户有责任根据需要添加任何错误处理。看看try语句和错误AppleScript语言指南中的语句。参见处理错误。此外,延迟的使用在适当的情况下,在事件之间可能需要命令,例如delay 0.5
,适当设置延迟的值。
你没有说你在什么操作系统版本,但这在Sierra上是有效的;不知道它是否适用于其他版本。根据评论,显然这应该适用于Movaje,但不适用于Catalina或更新版本。
你可能需要为你自己的设置调整这个,因为状态菜单项的顺序可能与我的不同。试着先把它设置为第1项,然后再按照你的方式设置正确的菜单。
当然,它假设你已经在System Preferences > Keyboard > Input Sources
中启用了"在菜单栏中显示输入菜单"。延迟是它工作所必需的,但你可以减少它们的持续时间。
它的作用是单击相关菜单,然后键入所需输入的第一个字母。您可能需要根据菜单中的其他项目来摆弄这个。它可以使用单个字母,但使用较长的字符串,例如'eng',有助于避免混淆。然后输入"返回"键来设置你的选择。使用字母来选择允许你绕过任何顺序或当前设置。
tell application "System Events"
tell application process "SystemUIServer"
click menu bar item 6 of menu bar 1
delay 0.3
keystroke "e"
delay 0.3
keystroke return
end tell
end tell