【问题标题】:In Applescript, how can I find out if a menu item is selected/focused?在 Applescript 中,如何确定菜单项是否被选中/聚焦?
【发布时间】:2010-09-09 07:14:21
【问题描述】:

我有一个用于 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} 的焦点。

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


我通过working around it 解决了我的问题。我仍然不知道如何检查菜单项是否被选中,所以我将保持这个话题打开。

【问题讨论】:

    标签: macos focus applescript menu


    【解决方案1】:

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

    注意:只有当正在检查其菜单的应用程序当前是最前面时,此测试才有效。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        使用它,您可以确定两件事。首先,focused 属性不在menu item 上,而是在menu item 内有一个text field 被聚焦。其次,菜单项有一个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 再次解决方案似乎要容易得多。

        【讨论】:

        • if searchField's focused is true then.. 这真的是苹果脚本语法吗?添加撇号?
        【解决方案4】:

        我刚刚发现需要自己在 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
        

        完成。

        这没有问题,可用于迭代文件。在我未来的自动化中,我可能需要多次这样做。

        祝你好运!

        【讨论】:

          【解决方案5】:

          这对我来说可以使用“selected”属性在两个菜单项之间切换,根据选择哪个菜单项:

          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
          

          【讨论】:

            猜你喜欢
            • 2023-04-02
            • 1970-01-01
            • 2010-11-13
            • 1970-01-01
            • 1970-01-01
            • 2014-01-01
            • 2023-02-26
            • 1970-01-01
            • 2018-05-09
            相关资源
            最近更新 更多