Hide Toolbar from script / macro?

The number of apps which respond to a toolbar right-click ( ^ click ) with contextual menus like this:

is large enough for me to guess that they are generated generically from some resource in Apple's frameworks.

Has anybody found a good generic approach to:

  1. finding and clicking the Hide Toolbar item on such menus, or
  2. achieving the same end by other means ?

( some apps seem to assign ⌥ ⌘ T to this, but not, alas, all … )

This seems to at least surface the contextual menu when the toolbar is showing, but not when it is hidden:

tell application "System Events"
    set lstProcs to application processes where name contains "TaskPaper"
    if lstProcs ≠ {} then
        set oProc to item 1 of lstProcs
        set lstWins to windows of oProc
        if lstWins ≠ {} then
            set oWin to item 1 of lstWins
            set lstToolbars to toolbars of oWin
            if lstToolbars ≠ {} then
                set oToolbar to item 1 of lstToolbars
                perform action "AXShowMenu" of oToolbar
            end if
        end if
    end if
end tell
1 Like