There are a few approaches you could take to this problem. Given that this is relatively straightforward, here is a basic approach. Begin by opening a numbers document and showing the inspector. The menus will change depending upon the state of the application, window or document.
What I did here was begin with menu bar 1, which is common to every application that has standard menus, get the UI elements of, and then repeat the process, extending the chain a link at a time based on the prior result.
tell application "Numbers"
tell application "System Events" to tell application process "Numbers"
UI elements of menu bar 1
--> menu bar item "View" of menu bar 1 of application process "Numbers" of application "System Events"
UI elements of menu bar item "View" of menu bar 1
--> {menu "View" of menu bar item "View" of menu bar 1 of application process "Numbers" of application "System Events"}
UI elements of menu "View" of menu bar item "View" of menu bar 1
--> menu item "Inspector" of menu "View" of menu bar item "View" of menu bar 1
UI elements of menu item "Inspector" of menu "View" of menu bar item "View" of menu bar 1
--> {menu "Inspector" of menu item "Inspector" of menu "View" of menu bar item "View" of menu bar 1 of application process "Numbers" of application "System Events"}
UI elements of menu "Inspector" of menu item "Inspector" of menu "View" of menu bar item "View" of menu bar 1
--> menu item "Show Inspector" of menu "Inspector" of menu item "Inspector" of menu "View" of menu bar item "View" of menu bar 1 of application process "Numbers" of application "System Events"
--> menu item "Hide Inspector" of menu "Inspector" of menu item "Inspector" of menu "View" of menu bar item "View" of menu bar 1 of application process "Numbers" of application "System Events"
end tell
end tell
Each of the commands actually returns a list. Many of the lists are large so I’ve only reported the key item here. When you get the ui elements of a menu item or menu bar item then the list should have but a single item; in such cases, I included the entire result. For the last command, I show the two relevant items but note that you don’t get them both in one go. As mentioned above, depending upon the window’s state, you will get one or the other. Oddly, I get the opposite of the expected result, ie when the inspector is already hidden, the above returns ‘hide inspector’. Go figure.
Often with menus, once you understand the structure, you can construct the full chain of items without any of the above.
Now that we know what we’re working with, here is a script that will open each menu in sequence and then check to see the state of the show/hide menu item. If the Inspector menu offers Hide Inspector, it will choose it, otherwise it will cancel out of the menus.
Note the brief delay after each press to allow time for the menu items to load.
tell application "Numbers"
activate
tell application "System Events" to tell application process "Numbers"
set mui1 to menu bar item "View" of menu bar 1
perform action "AXPress" of mui1
delay 0.1
set mui2 to menu item "Inspector" of menu "View" of mui1
perform action "AXPress" of mui2
delay 0.1
set mui3 to menu "Inspector" of mui2
tell mui3
delay 0.1
set lame to last menu item of mui3
-- log lame
try
if lame is menu item "Hide Inspector" then
perform action "AXPress" of last menu item of mui3
end if
on error
perform action "AXCancel" of last menu item of mui3
end try
end tell
end tell
end tell
-- Result:
--> action "AXPress" of menu item "Hide Inspector" of menu "Inspector" of menu item "Inspector" of menu "View" of menu bar item "View" of menu bar 1 of application process "Numbers" of application "System Events"
While it is rarely this straightforward, you can use the same process to root through a window’s elements in order to script that. Somewhere —I think— there is a post hereabouts that works with some elements of Numbers’ inspectors to change some formatting.
Finally, here is an alternate approach that avoids simulating menu clicking and instead simply gets the last menu item of the Inspector menu and if it is ‘hide’, simulates key presses using key codes to use the keyboard shortcut to hide the inspector.
tell application "Numbers"
activate
tell application "System Events" to tell application process "Numbers"
set mui1 to menu bar item "View" of menu bar 1
set mui2 to menu item "Inspector" of menu "View" of mui1
set mui3 to menu "Inspector" of mui2
tell mui3
set lame to last menu item of mui3
if name of lame is "Hide Inspector" then -- otherwise "Show Inspector"
-- log name of lame
key code 34 using {command down, option down} -- command-shift-I
end if
end tell
end tell
end tell
Hope this helps.