How do you iterate through menus in AppleScript?
applescript, iterator, loops, safari, web-inspector
Solution
Here's an example. Suppose I want to click the "Show Web Inspector" menu item under the "Develop" menu in Safari. I will first get all of the UIElements in the Develop menu, then iterate over them looking for the UIElement with the proper name. Once found I can click it.
Note that the secret to this is getting the "entire contents" of some UIElement, in this case the Develop menu. That gives me a list of every UIElement in the menu so that I can iterate through them and find whatever I want.
Also note that I have a try block around the if statement. That's because some UIElements don't have a name and it errors, so this just ignores those errors.
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
set developMenu to menu bar item "Develop" of menu bar 1
set allUIElements to entire contents of developMenu
repeat with anElement in allUIElements
try
if name of anElement is "Show Web Inspector" then
click anElement
exit repeat
end if
end try
end repeat
end tell
end tell
Problem
Specifically, when I'm debugging web stuff in iOS, I can start Safari Web Inspectors by clicking on the following menu items in the Safari menu bar: - Develop->iPad Simulator->Safari->my web page's title - Develop->iPad Name(mine is called Summer)->Safari->my web page's title I'd like to iterate over the Safari menus and activate any items in the Develop menu that contain a common substring I use in my web page titles.