如何在AppleScript中迭代菜单?

时间:2013-08-06 02:06:11

标签: loops safari iterator applescript web-inspector

具体来说,当我在iOS中调试Web内容时,我可以通过单击Safari菜单栏中的以下菜单项来启动Safari Web Inspectors:

  • 开发 - > iPad模拟器 - > Safari->我的网页标题
  • 开发 - > iPad名称(我的名字叫夏天) - > Safari->我的网页标题

我想迭代Safari菜单并激活“开发”菜单中包含我在网页标题中使用的公共子字符串的任何项目。

2 个答案:

答案 0 :(得分:5)

这是一个例子。假设我想单击Safari中“Develop”菜单下的“Show Web Inspector”菜单项。我将首先在Develop菜单中获取所有UIElements,然后迭代它们以查找具有正确名称的UIElement。一旦找到,我可以点击它。

请注意,这样做的秘诀是获取某些UIElement的“全部内容”,在本例中为“开发”菜单。这给了我菜单中每个UIElement的列表,这样我就可以遍历它们并找到我想要的任何内容。

另请注意,我在if语句周围有一个try块。那是因为有些UIElements没有名称而且有错误,所以这只是忽略了那些错误。

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

答案 1 :(得分:2)

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