我们如何使用 applescript 为 ios 模拟器重置 safari devtools 的大小?

how do we reset the size of safari devtools with applescript for ios simulators?

我们如何使用模拟器的 apple-script 重置 safari 开发工具的大小? url 可以是随机的,也可以是任何东西(任何站点)。从截图中可以看出,我们在模拟器的 safari devtools 中有一个设置选项,我们可以在其中使用“缩放”选项设置 devtools 的大小。我希望它设置为 100%,而 applescript 做同样的工作。

我正在尝试的脚本如下所示,它试图激活 safari,然后转到模拟器设备并单击 url。然后我陷入了如何单击 safari devtools 上的设置选项卡,最后单击缩放选项以使用 applescript

调整 devtools 的大小

图片如下:

Please see this image

tell application "System Events"
tell application process "Safari"
    if not (exists menu bar item "Develop" of menu bar 1) then return
    tell menu 1 of menu bar item "Develop" of menu bar 1
        set simulatorMenuName to the name of (menu items whose name starts with "Simulator") as string
        if simulatorMenuName is equal to "" then return
        set simulatorMenuNameMenuItems to the name of menu items of menu 1 of menu item simulatorMenuName
        if item 1 of simulatorMenuNameMenuItems is not "Safari" then return
        repeat with i from 1 to count simulatorMenuNameMenuItems
            if item i of simulatorMenuNameMenuItems is equal to missing value then
                set menuItemNumber to i - 1
                exit repeat
            end if
        end repeat
        tell menu 1 of menu item simulatorMenuName to click menu item menuItemNumber
    end tell
end tell

结束讲述

我目前无法在与您相同的环境中进行测试,但是,他 example AppleScript code,如下所示,已在 Script EditormacOS CatalinaSafari 版本 14.0.3(15610.4.3.1.6、15610),系统偏好设置中的语言和地区设置设置为英语(美国)- 初级 并且为我工作没有问题1

  • 1 系统偏好设置 > 安全和隐私[= 中假定必要且适当的设置97=] > 隐私 已根据需要 set/addressed。

示例 AppleScript 代码:

tell application "Safari" to activate
delay 0.2
tell application "System Events"
    tell front window of application process "Safari"
        
        if (value of pop up button 1 of group 19 of ¬
            group 4 of UI element 1 of scroll area 1 of ¬
            group 1 of group 1) is not "100%" then
            
            click ¬
                pop up button 1 of group 19 of ¬
                group 4 of UI element 1 of ¬
                scroll area 1 of group 1 of group 1
            
            delay 0.1
            
            click ¬
                menu item "100%" of menu 1 of group 1
            
        end if
        
    end tell
end tell

备注:

示例 AppleScript code 假设 front [= Safari 的 170=] 已经加载到目标 页面 和/或 Web Inspector tab/window.

Web Inspector 的目标 tab 似乎不是 JavaScript 可通过 AppleScript 编写脚本,但是,它可以使用 UI Scripting 编写脚本。请注意,UI 脚本 充其量是 笨拙的 ,并且由于 层次结构 [=54] 的变化而容易失败=]UI element structure of the OS and or application being脚本化。可能需要对 示例 AppleScript 代码 进行更改,以便您在 macOS Big Sur,或任何其他版本的 macOS 和或 Safari 其他比它被测试和工作的条件。

您可以使用Accessibility Inspector检查层级UI元素结构 在您正在使用的 macOSSafari 的当前版本下。您还可以使用 AppleScript 查询各种 分层 UI 元素 结构 以确定哪个它是 pop up button 按钮 .

的正确按钮


注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5,适当设置延迟