views:

1756

answers:

3

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.

A: 

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.

tjw
+1  A: 

The built in key shortcut cmd-? 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.

Ken
+1  A: 

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!

Steve Jones