在Applescript中,如何确定菜单项是否被选中/聚焦?

时间:2020-12-15 15:05:37

I have a script for OS X 10.5 that focuses the Search box in the Help menu of any application. I have it on a key combination and, much like Spotlight, I want it to toggle when I run the script. So, I want to detect if the search box is already focused for typing, and if so, type Esc instead of clicking the Help menu.

我有一个OS X 10.5脚本,它将搜索框的焦点集中在任何应用程序的“帮助”菜单中。我有一个关键组合,就像Spotlight一样,我希望它在我运行脚本时切换。因此,我想检测搜索框是否已经集中用于键入,如果是,请键入Esc而不是单击“帮助”菜单。

Here is the script as it stands now:

这是现在的脚本:

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

And I'm thinking of something like this:

而我正在考虑这样的事情:

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

... but I get this error:

...但是我收到了这个错误:

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"}.

应用程序“系统事件”的应用程序“脚本编辑器”的菜单栏1的菜单栏项“帮助”的菜单“帮助”的菜单项目1无法集中。

So is there a way I can get my script to detect whether the search box is already focused?

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


I solved my problem by working around it. I still don't know how to check if a menu item is selected though, so I will leave this topic open.

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

4 个解决方案

#1


2  

Using /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app you can use the built-in accessibility system to look at properties of the UI element under the mouse. Take special note of the cmd-F7 action to lock focus on an element and the Refresh button. Sadly the element and property names don't directly match those in the script suite, but you can look at the dictionary for System Events or usually guess the right terminology.

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

Using this you can determine two things. First, the focused property isn't on the menu item, but rather there is a text field within the menu item that is focused. Second, the menu item has a selected property.

使用此功能,您可以确定两件事。首先,聚焦属性不在菜单项上,而是在菜单项中有一个聚焦的文本字段。其次,菜单项具有选定的属性。

With this, I came up with:

有了这个,我想出了:

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

Though this still doesn't work. The key event isn't firing as far as I can tell, so something may still be hinky with the focused property on the text field.

虽然这仍然不起作用。据我所知,关键事件并未触发,因此文本字段中的焦点属性可能仍然存在一些问题。

Anyway, your click again solution seems much easier.

无论如何,你的点击再次解决方案似乎更容易。

#2


4  

The built in key shortcut Cmd-? (Cmd-Shift-/) already behaves like this. It moves key focus to the help menu's search field if it is not already focused, and otherwise dismisses the menu.

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

#3


2  

You need to use attribute AXMenuItemMarkChar.

您需要使用属性AXMenuItemMarkChar。

Example:

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

If the menu item is checked, the return value is , otherwise it is missing value.

如果选中菜单项,则返回值为✓,否则为缺失值。

Note: This test only works if the application whose menus are being inspected is currently frontmost.

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

#4


1  

I just came across the need to do this myself for some file processing in Illustrator.

我只是想在Illustrator中进行一些文件处理。

Here is what I came up with:

这是我想出的:

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

Done.

This worked with no problem and could be used to iterate a file. I'll probably have to do this many more times in my future automation.

这没有问题,可以用来迭代文件。在未来的自动化中,我可能不得不多做这件事。

Good luck!

#1


2  

Using /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app you can use the built-in accessibility system to look at properties of the UI element under the mouse. Take special note of the cmd-F7 action to lock focus on an element and the Refresh button. Sadly the element and property names don't directly match those in the script suite, but you can look at the dictionary for System Events or usually guess the right terminology.

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

Using this you can determine two things. First, the focused property isn't on the menu item, but rather there is a text field within the menu item that is focused. Second, the menu item has a selected property.

使用此功能,您可以确定两件事。首先,聚焦属性不在菜单项上,而是在菜单项中有一个聚焦的文本字段。其次,菜单项具有选定的属性。

With this, I came up with:

有了这个,我想出了:

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

Though this still doesn't work. The key event isn't firing as far as I can tell, so something may still be hinky with the focused property on the text field.

虽然这仍然不起作用。据我所知,关键事件并未触发,因此文本字段中的焦点属性可能仍然存在一些问题。

Anyway, your click again solution seems much easier.

无论如何,你的点击再次解决方案似乎更容易。

#2


4  

The built in key shortcut Cmd-? (Cmd-Shift-/) already behaves like this. It moves key focus to the help menu's search field if it is not already focused, and otherwise dismisses the menu.

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

#3


2  

You need to use attribute AXMenuItemMarkChar.

您需要使用属性AXMenuItemMarkChar。

Example:

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

If the menu item is checked, the return value is , otherwise it is missing value.

如果选中菜单项,则返回值为✓,否则为缺失值。

Note: This test only works if the application whose menus are being inspected is currently frontmost.

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

#4


1  

I just came across the need to do this myself for some file processing in Illustrator.

我只是想在Illustrator中进行一些文件处理。

Here is what I came up with:

这是我想出的:

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

Done.

This worked with no problem and could be used to iterate a file. I'll probably have to do this many more times in my future automation.

这没有问题,可以用来迭代文件。在未来的自动化中,我可能不得不多做这件事。

Good luck!