如何提升Safari 15下新建标签页的AppleScript脚本可靠性
Safari 15 AppleScript 新建标签加载失败修复方案
这一故障是Safari 15的原生兼容性缺陷,触发原因为创建新标签时同步传入URL参数,和Safari新标签页的初始化流程产生时序冲突,导致页面加载被强制中断跳转至起始页。
最简修复方案
仅需拆分「创建新标签」和「赋值URL」两个动作即可解决,不需要额外加延迟、重试逻辑,代码复杂度和原版本基本一致:
tell application "Safari" set theWindow to first window -- 先创建空新标签,不传入URL参数 set newTab to make new tab at end of tabs of theWindow -- 单独给新标签设置URL set URL of newTab to theUrl set current tab of theWindow to newTab set miniaturized of theWindow to false set tabTotal to count of tabs of theWindow end tell
备选极简方案
如果需要更短的代码,可以直接调用Safari原生的open指令,会自动在指定窗口生成新标签打开链接,完全避开新建标签的时序问题:
tell application "Safari" set theWindow to first window open theUrl in theWindow set miniaturized of theWindow to false set tabTotal to count of tabs of theWindow end tell
内容的提问来源于stack exchange,提问作者user3579815
相关产品推荐
相关产品推荐

