Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you iterate through menus in AppleScript?

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.

like image 905
basicallyrun Avatar asked Mar 11 '26 06:03

basicallyrun


2 Answers

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
like image 81
regulus6633 Avatar answered Mar 13 '26 21:03

regulus6633


tell application "System Events" to tell process "Finder"
    set frontmost to true
    tell menu 1 of menu item "Arrange by" of menu 1 of menu bar item "View" of menu bar 1
        click (menu items where its name contains "a")
    end tell
end tell
tell application "System Events" to tell process "Finder"
    set frontmost to true
    repeat with m in (get menu 1 of menu items of menu 1 of menu bar item "View" of menu bar 1)
        set m to contents of m
        if m is not missing value then
            click (menu items of m where name contains "a")
        end if
    end repeat
end tell
like image 40
Lri Avatar answered Mar 13 '26 19:03

Lri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!