我有一个适用于 OS X 10.5 的脚本,可以在任何应用程序的“帮助”菜单中聚焦“搜索”框。我把它放在一个组合键上,就像 Spotlight 一样,我希望它在我运行脚本时进行切换。因此,我想检测搜索框是否已经集中用于键入,如果是,请键入 Esc 而不是单击“帮助”菜单。
这是现在的脚本:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
click helpMenuItem
end tell
end tell
我在想这样的事情:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
set searchBox to menu item 1 of menu of helpMenuItem
if (searchBox's focused) = true then
key code 53 -- type esc
else
click helpMenuItem
end if
end tell
end tell
...但我收到此错误:
Can’t get focused of {menu item 1 of menu "Help" of menu bar item "Help" of menu bar 1 of application process "Script Editor" of application "System Events"}.
那么有没有办法让我的脚本检测搜索框是否已经聚焦?
我通过 working around it 解决了我的问题.我仍然不知道如何检查菜单项是否被选中,所以我将保留这个主题。
最佳答案
您需要使用属性 AXMenuItemMarkChar
.
例子:
tell application "System Events"
tell process "Cisco Jabber"
set X to (value of attribute "AXMenuItemMarkChar" of menu item "Available" of menu "Status" of menu item "Status" of menu "File" of menu bar item "File" of menu bar 1) is "✓" -- check if Status is "Availible"
end tell
end tell
如果选中菜单项,返回值为
✓
, 否则为 missing value
.注意:此测试仅在正在检查其菜单的应用程序当前位于最前面时才有效。
关于macos - 在 Applescript 中,如何确定菜单项是否被选中/聚焦?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69030/