Loops 如何在AppleScript中遍历菜单?

Loops 如何在AppleScript中遍历菜单?,loops,safari,iterator,applescript,web-inspector,Loops,Safari,Iterator,Applescript,Web Inspector,具体来说,当我在iOS中调试web内容时,我可以通过单击Safari菜单栏中的以下菜单项启动Safari web Inspector: 开发->iPad模拟器->Safari->我的网页标题 开发->iPad名称(我的名字叫Summer)->Safari->我的网页标题 我想迭代Safari菜单,并激活“开发”菜单中包含我在网页标题中使用的公共子字符串的任何项目 tell application "System Events" to tell process "Finder" set

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

  • 开发->iPad模拟器->Safari->我的网页标题
  • 开发->iPad名称(我的名字叫Summer)->Safari->我的网页标题
我想迭代Safari菜单,并激活“开发”菜单中包含我在网页标题中使用的公共子字符串的任何项目

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

这里有一个例子。假设我想单击Safari中“开发”菜单下的“显示Web检查器”菜单项。我将首先获取develope菜单中的所有UIElement,然后遍历它们以查找具有正确名称的UIElement。一旦找到,我可以点击它

请注意,其中的秘密是获取某些UIElement的“全部内容”,在本例中是Develop菜单。这给了我菜单中每个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

这太好了,谢谢劳里。为了使您的第二个脚本适应web inspector场景,“Finder”更改为“Safari”,而“View”更改为“Develop”。谢谢regulus!我刚刚将“anElement的名称是”切换为“anElement的名称包含”并将“Show Web Inspector”切换为我的网页标题的一个常用子字符串,我很乐意使用。很高兴提供帮助。当然,您还需要删除“退出重复”。
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