In Applescript, how can I find out if a menu item

2020-06-20 04:49发布

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.

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

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条回答
闹够了就滚
2楼-- · 2020-06-20 05:40

You need to use attribute 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.

查看更多
姐就是有狂的资本
3楼-- · 2020-06-20 05:47

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.

查看更多
一夜七次
4楼-- · 2020-06-20 05:49

I just came across the need to do this myself for some file processing in 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!

查看更多
叼着烟拽天下
5楼-- · 2020-06-20 05:53

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.

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.

查看更多
登录 后发表回答