【问题标题】:How do you iterate through menus in AppleScript?您如何遍历 AppleScript 中的菜单?
【发布时间】:2013-08-06 02:06:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: loops safari iterator applescript web-inspector


    【解决方案1】:

    这是一个例子。假设我想单击 Safari 中“开发”菜单下的“显示 Web 检查器”菜单项。我将首先在“开发”菜单中获取所有 UIElement,然后遍历它们以查找具有正确名称的 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
    

    【讨论】:

    • 感谢轩辕!我刚刚将“一个元素的名称是”切换到“一个元素的名称包含”和“显示 Web 检查器”切换到我的网页标题的公共子字符串,我很高兴。
    • 很高兴为您提供帮助。当然,您也需要删除“退出重复”。
    【解决方案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
    

    【讨论】:

    • 这太好了,谢谢劳里。为了使您的第二个脚本适应网络检查器场景,“Finder”更改为“Safari”,“View”更改为“Develop”。
    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 2012-09-26
    • 2011-10-27
    • 2019-06-24
    • 2021-08-21
    • 2018-09-24
    相关资源
    最近更新 更多