我有一个用于 OS X 10.5 的脚本,它将搜索框集中在任何应用程序的帮助菜单中。我把它放在一个组合键上,就像 Spotlight 一样,我希望它在我运行脚本时切换。所以,我想检测搜索框是否已经专注于输入,如果是,请输入 Esc 而不是单击帮助菜单。
这是现在的脚本:
1 2 3 4 5 6
| 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 |
我正在考虑这样的事情:
1 2 3 4 5 6 7 8 9 10 11
| 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"}.
那么有没有办法让我的脚本检测搜索框是否已经聚焦?
我解决了我的问题。我仍然不知道如何检查菜单项是否被选中,所以我将保持这个话题打开。
你需要使用属性AXMenuItemMarkChar.
示例:
1 2 3 4 5
| 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。
注意:此测试仅在其菜单被检查的应用程序当前位于最前面时才有效。
内置的快捷键 Cmd-? (Cmd-Shift-/) 已经像这样了。如果尚未获得焦点,它将键焦点移至帮助菜单的搜索字段,否则将关闭菜单。
我刚刚发现需要自己在 Illustrator 中进行一些文件处理。
这是我想出的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| tell application"Adobe Illustrator"
activate
tell application"System Events"
tell process"Illustrator"
set frontmost to true
set activeMenuItem to enabled of menu item"Unlock All" of menu"Object" of menu bar item"Object" of menu bar 1
if activeMenuItem is true then
tell me to beep 3
else
tell me to beep 2
end if
end tell
end tell
end tell |
完成。
这没有问题,可用于迭代文件。在我未来的自动化中,我可能不得不多次这样做。
祝你好运!
使用 /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app,您可以使用内置的辅助功能系统查看鼠标下 UI 元素的属性。请特别注意 cmd-F7 操作以将焦点锁定在元素和刷新按钮上。遗憾的是元素和属性名称与脚本套件中的名称不直接匹配,但您可以查看系统事件的字典或通常猜测正确的术语。
使用它可以确定两件事。首先,focused 属性不在 menu item 上,而是在 menu item 内有一个 text field 被聚焦。其次,菜单项有一个 selected 属性。
有了这个,我想出了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| tell application"System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item"Help" of menu bar 1
-- Use reference form to avoid building intermediate object specifiers, which Accessibility apparently isn't good at resolving after the fact.
set searchBox to a reference to menu item 1 of menu of helpMenuItem
set searchField to a reference to text field 1 of searchBox
if searchField's focused is true then
key code 53 -- type esc
else
click helpMenuItem
end if
end tell
end tell |
虽然这仍然行不通。据我所知,关键事件没有触发,因此文本字段上的 focused 属性可能仍然存在问题。
无论如何,您的 click 再次解决方案似乎要容易得多。
这对我来说可以使用 "selected" 属性在两个菜单项之间切换,根据选择的一个菜单项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| tell application"System Preferences"
reveal anchor"keyboardTab" of pane"com.apple.preference.keyboard"
end tell
tell application"System Events" to tell process"System Preferences"
tell pop up button 2 of tab group 1 of window 1
click
delay 0.2
set appControl to menu item"App Controls" of menu 1
set fKeys to menu item"F1, F2, etc. Keys" of menu 1
if selected of appControl is true then
click fKeys
else
click appControl
end if
end tell
end tell |