1. Use GUI Scripting. It's tedious, but it works. For example, this script returns all the children of the Finder application when one Finder window is open:
That is what I tried to do with the menus
get name of menu bar items of menu bar 1 => {"Apple", "Mail", "File", "Edit", "View", "Mailbox", "Message", "Format", "Window", "Help"}
And then I tried to get the items in "File" but you can't get them, you have to change the syntax AND add "menu 1". As far as I can tell, there is no way to use applescript to 'discover' this syntax.
get name of menu items of menu 1 of menu bar item "File" of menu bar 1
(note: it's not "get name of menu bar items" but "get name of menu items", and you can't get them for menu bar item "File", you have to get them from menu 1 of menu bar item "File"). This seems unnecessarily difficult, but I guess since it all happens with point and clicks in Xcode, Apple has no motivation to make this easier in Applescript?
You're right, and I should have spelled that out.
There are several UI elements in Mac OS that do not exist unless and until their "parent" element is opened. Menus (both in the menubar and contextual menus) are in that category. It's because the underlying OS routines for the menubar and menus do not create the child object until the parent object is opened. To handle this in AppleScript, you have to do something like opening the menubar or menu first in order to bring the children into existence, and then open one of the children. This presupposes that you know something about the mechanism.
Changing how the operating system works just to make AppleScript a little more transparent would be a case of the tail wagging the dog, I think.
UI Browser handles this with several mechanisms, all explained in the Help book. |