首页 > 解决方案 > 我们如何使用适用于 ios 模拟器的 applescript 重置 safari devtools 的大小?

问题描述

我们如何使用模拟器的苹果脚本重置 safari 开发工具的大小?网址可以是随机的,也可以是任何东西(任何网站)。从截图中可以看出,我们在 safari devtools for Simulators 中有一个设置选项,我们可以在其中使用“zoom”选项设置 devtools 的大小。我希望它设置为 100%,applescript 做同样的工作。

我正在尝试的这个脚本如下所示正在尝试激活 safari,然后转到模拟器设备并单击 url。然后我被困在如何单击 safari devtools 上的设置选项卡上,最后在缩放选项上使用 applescript 调整 devtools

下面是图片:

请看这张图片

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

结束告诉

标签: safariapplescriptios-simulatordevtools

解决方案


我目前无法在与您相同的环境中进行测试,但是,下面显示的示例 AppleScript 代码已在macOS CatalinaSafari版本 14.0.3(15610.4.3.1.6、15610)下的脚本编辑器中进行了测试系统偏好设置中的语言和区域设置设置为英语(美国) - 主要并为我工作,没有问题1

  • 1 假设系统偏好设置>安全和隐私>隐私中的必要和适当设置已根据需要进行设置/解决。

示例 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 代码假定Safari前窗口已加载到目标页面和/或Web Inspector tab/window

Web Inspector的目标选项卡似乎无法通过AppleScript编写JavaScript脚本,但是可以使用UI Scripting 编写脚本。请注意,UI 脚本充其量是杂乱无章的,并且由于操作系统和/或正在编写脚本的应用程序的分层UI 元素结构的变化而容易失败。您在macOS Big Sur或任何其他版本的macOS/ Safari不是经过测试和工作的。

您可以使用Accessibility Inspector检查当前版本的macOSSafari下的分层UI 元素结构。您还可以使用AppleScript查询各种分层UI 元素结构,以确定哪个最适合按钮pop up button




注意:示例 AppleScript 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript 语言指南中的try 语句错误 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟 delay 0.5适当设置。


推荐阅读