如何通过AppleScript重置iOS模拟器对应Safari DevTools的缩放尺寸
通过AppleScript重置iOS模拟器Safari DevTools缩放比例为100%的实现方案
你已编写的脚本已经实现了激活Safari、从Develop菜单打开对应模拟器页面开发者工具的功能,只需补充后续操作DevTools设置的代码即可。
完整可运行脚本
tell application "System Events" tell application process "Safari" set frontmost to true delay 0.5 -- 等待Safari激活 -- 以下是原有打开DevTools的逻辑 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 delay 1 -- 等待DevTools窗口加载完成 -- 以下是新增的操作DevTools缩放的逻辑 -- 定位DevTools窗口,窗口名称通常包含调试页面地址或者「Web Inspector」字样 set devToolsWindows to every window whose name contains "Web Inspector" or name contains "开发" if (count of devToolsWindows) = 0 then return set devToolWindow to item 1 of devToolsWindows -- 定位设置按钮(齿轮图标,中文环境描述为「设置」,英文为「Settings」) set settingButton to first button of toolbar 1 of devToolWindow whose description contains "设置" or description contains "Settings" click settingButton delay 0.3 -- 定位缩放选项下的100%按钮点击 -- 中文环境匹配「缩放」和「100%」,英文环境替换为「Zoom」和「100%」 set zoom100Item to first menu item of menu 1 of settingButton whose name contains "100%" and (name contains "缩放" or name contains "Zoom") click zoom100Item end tell end tell
注意事项
- 运行脚本前需要给运行载体(脚本编辑器、终端或者你自定义的运行工具)授予辅助功能权限,权限入口在「系统设置→隐私与安全性→辅助功能」,勾选对应应用即可
- 如果你的Safari是英文界面,把脚本中对应的中文关键词「设置」「缩放」替换为英文「Settings」「Zoom」即可
- 如果运行时出现元素找不到的报错,可以用macOS自带的脚本编辑器的UI元素浏览器,查看当前Safari版本DevTools的元素描述,微调匹配关键词即可
内容的提问来源于stack exchange,提问作者Ritesh Kumar
相关产品推荐
相关产品推荐

