在Applescript中,如何查找菜单项是否被选中/关注?

问题描述:

我有一个OS X 10.5的脚本,它将搜索框关注于任何应用程序的帮助菜单。我在一个组合键上,它非常像Spotlight,我希望它在我运行脚本时切换。因此,我想检测搜索框是否已经专注于输入,如果是,请键入Esc而不是单击“帮助”菜单。在Applescript中,如何查找菜单项是否被选中/关注?

这里是脚本目前的情况是:

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解决了我的问题。我仍然不知道如何检查菜单项是否被选中,因此我将打开此主题。

使用/开发人员/应用程序/实用程序/辅助功能工具/辅助功能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再次解决方案似乎更容易。

+0

`如果searchField的焦点是真的然后`..是真的苹果脚本语法?添加一个撇号? – abbood 2017-02-17 03:26:03

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

我刚才遇到了需要自己做这些以便在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 

完成。

这工作没有问题,可用于迭代文件。在将来的自动化中,我可能不得不多次这么做。

祝你好运!

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

注意:此测试仅适用于正在检查菜单的应用程序当前为最前面的