我有一个适用于 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

...但我收到此错误:

无法获得焦点{菜单栏菜单“帮助”的菜单项 1 应用程序“系统事件”的应用程序进程“脚本编辑器”的菜单栏 1 的“帮助”项}。

那么有没有办法让我的脚本检测搜索框是否已经聚焦?


我解决了我的问题 围绕它工作. 。我仍然不知道如何检查是否选择了菜单项,所以我将保留这个主题。

有帮助吗?

解决方案

使用 /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app 您可以使用内置的辅助功能系统来查看鼠标下 UI 元素的属性。请特别注意将焦点锁定在元素上的 cmd-F7 操作和“刷新”按钮。遗憾的是,元素和属性名称与脚本套件中的名称不直接匹配,但您可以查看系统事件字典或通常猜测正确的术语。

使用它您可以确定两件事。首先, focused 属性不在 menu item, ,而是有一个 text fieldmenu item 那是集中的。其次,菜单项有一个 selected 财产。

有了这个,我想出了:

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 再次解决方案似乎更容易。

其他提示

内置快捷键 命令-? (Cmd-Shift-/)已经表现得像这样。如果尚未获得焦点,它将键焦点移至帮助菜单的搜索字段,否则将关闭菜单。

您需要使用属性 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.

笔记:仅当正在检查其菜单的应用程序当前处于 最前面的.

我刚刚发现需要自己在 Illustrator 中进行一些文件处理。

这是我想出的:

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

完毕。

这没有问题,可以用来迭代文件。在未来的自动化中,我可能需要多次执行此操作。

祝你好运!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top