In Applescript, how can I find out if a menu item is selected / focused?

I have a script for OS X 10.5 that focuses the search field on the Help menu of any application. I have a keyboard shortcut and, like Spotlight, I want it to switch when I run the script. Thus, I want to determine if the search box is already focused for input, and if so, enter Esc instead of clicking on the help menu.

Here is the script as of 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 am 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 focused) = true then key code 53 -- type esc else click helpMenuItem end if end tell end tell 

... but I get this error:

It is impossible to concentrate on {menu 1 of the "Help" menu item of the "Help" menu item of the menu bar 1 of the "Script Editor" application process of the "System Events" application}.

So, is there a way to make my script detect that the search box is already focused?


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

+5
source share
4 answers

Usage / Developer / Applications / Utilities / Special tools / Accessibility Inspector.app you can use the built-in accessibility system to view the properties of a user interface element under the mouse. Pay particular attention to the cmd-F7 action to lock focus on the item and the Refresh button. Unfortunately, the names of the elements and properties do not directly match the names in the script package, but you can look at the dictionary for system events or, as a rule, guess the correct terminology.

Using this, you can define two things. Firstly, the focused property is not in the menu item , but there is a text field within the menu item that is focused. Secondly, 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 focused is true then key code 53 -- type esc else click helpMenuItem end if end tell end tell 

Although this still does not work. The key event does not fire, as far as I can tell, so something in the text field may still be more flagged.

Anyway, your click solution looks a lot easier.

+2
source

Built-in key shortcut Cmd-? ( Cmd-Shift- / ) already behaves as follows. It moves the key focus in the search field of the help menu if it is not already focused, and otherwise rejects the menu.

+4
source

You need to use the AXMenuItemMarkChar attribute.

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 a missing value .

Note. This test only works if the application whose menus are being checked is currently at the very beginning.

+2
source

I just ran into 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 without problems and could be used to iterate the file. I will probably have to do this many more times in my future automation.

Good luck

+1
source

All Articles