<p>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.</p>

<p>Here is the script as it stands now:</p>

<pre><code>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
</code></pre>

<p>And I'm thinking of something like this:</p>

<pre><code>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
</code></pre>

<p>... but I get this error:</p>

<blockquote>
  <p>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"}.</p>
</blockquote>

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

<hr>

<p>I solved my problem by <a href="https://stackoverflow.com/questions/69391/">working around it</a>.  I still don't know how to check if a menu item is selected though, so I will leave this topic open.</p>

[{'answer_id': 69404, 'author': 'tjw', 'author_id': 11029, 'author_profile': 'https://Stackoverflow.com/users/11029', 'pm_score': 3, 'selected': True, 'text': '<p>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.</p>\n\n<p>Using this you can determine two things.  First, the <code>focused</code> property isn\'t on the <code>menu item</code>, but rather there is a <code>text field</code> within the <code>menu item</code> that is focused.  Second, the menu item has a <code>selected</code> property.</p>\n\n<p>With this, I came up with:</p>\n\n<pre><code>tell application "System Events"\n    tell (first process whose frontmost is true)\n        set helpMenuItem to menu bar item "Help" of menu bar 1\n\n        -- Use reference form to avoid building intermediate object specifiers, which Accessibility apparently isn\'t good at resolving after the fact.\n        set searchBox to a reference to menu item 1 of menu of helpMenuItem\n        set searchField to a reference to text field 1 of searchBox\n\n        if searchField\'s focused is true then\n            key code 53 -- type esc\n        else\n            click helpMenuItem\n        end if\n    end tell\nend tell\n</code></pre>\n\n<p>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 <code>focused</code> property on the text field.</p>\n\n<p>Anyway, your <code>click</code> again solution seems much easier.</p>\n'}, {'answer_id': 90626, 'author': 'Ken', 'author_id': 17320, 'author_profile': 'https://Stackoverflow.com/users/17320', 'pm_score': 2, 'selected': False, 'text': "<p>The built in key shortcut <kbd>Cmd-?</kbd> (<kbd>Cmd-Shift-/</kbd>) 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.</p>\n"}, {'answer_id': 1545862, 'author': 'Steve Jones', 'author_id': 187427, 'author_profile': 'https://Stackoverflow.com/users/187427', 'pm_score': 2, 'selected': False, 'text': '<p>I just came across the need to do this myself for some file processing in Illustrator.</p>\n\n<p>Here is what I came up with:</p>\n\n<pre><code>tell application "Adobe Illustrator"\nactivate\ntell application "System Events"\n    tell process "Illustrator"\n        set frontmost to true\n        set activeMenuItem to enabled of menu item "Unlock All" of menu "Object" of menu bar item "Object" of menu bar 1\n        if activeMenuItem is true then\n            tell me to beep 3\n        else\n            tell me to beep 2\n        end if\n    end tell\nend tell\nend tell\n</code></pre>\n\n<p>Done.</p>\n\n<p>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.</p>\n\n<p>Good luck!</p>\n'}, {'answer_id': 35948626, 'author': 'Chester Rieman', 'author_id': 4821121, 'author_profile': 'https://Stackoverflow.com/users/4821121', 'pm_score': 2, 'selected': False, 'text': '<p>You need to use attribute <code>AXMenuItemMarkChar</code>.</p>\n\n<p>Example:</p>\n\n<pre><code>tell application "System Events"\n    tell process "Cisco Jabber"\n        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"    \n    end tell\nend tell\n</code></pre>\n\n<p>If the menu item is checked, the return value is <code>✓</code>, otherwise it is <code>missing value</code>.</p>\n\n<p>Note: This test only works if the application whose menus are being inspected is currently <em>frontmost</em>.</p>\n'}, {'answer_id': 65835776, 'author': 'Tao Starbow', 'author_id': 1578589, 'author_profile': 'https://Stackoverflow.com/users/1578589', 'pm_score': 0, 'selected': False, 'text': '<p>This worked for me to toggle between two menu items, based on which one is selected, using the &quot;selected&quot; property:</p>\n<pre><code>tell application &quot;System Preferences&quot;\n    reveal anchor &quot;keyboardTab&quot; of pane &quot;com.apple.preference.keyboard&quot;\nend tell\ntell application &quot;System Events&quot; to tell process &quot;System Preferences&quot;\n    tell pop up button 2 of tab group 1 of window 1\n        click\n        delay 0.2\n        set appControl to menu item &quot;App Controls&quot; of menu 1\n        set fKeys to menu item &quot;F1, F2, etc. Keys&quot; of menu 1\n        if selected of appControl is true then\n            click fKeys\n        else\n            click appControl\n        end if\n    end tell\nend tell\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/69030', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10906/']