如何在macOS上通过AppleScript打开指定尺寸Safari新窗口且无闪烁不篡改默认设置
优化后的Safari指定尺寸新窗口AppleScript实现
核心优化思路:全程后台执行参数调整、默认值恢复操作,所有临时窗口对用户不可见,彻底消除闪烁与冗余弹窗。
on run set targetURL to "https://…你的目标URL…" set presetBounds to {21, 46, 621, 191} -- 自定义的预设窗口{左上x, 左上y, 右下x, 右下y} tell application "Safari" -- 1. 静默获取原有默认窗口尺寸 set originalBounds to missing value try -- 若Safari已有打开窗口,直接复用现有窗口尺寸作为基准 if exists front window then set originalBounds to bounds of front window end if end try -- 无可用参考窗口时,创建完全不可见的临时窗口获取系统默认尺寸 if originalBounds is missing value then set tempWindow to make new document with properties {URL:"favorites://"} set visible of tempWindow to false -- 临时窗口完全不可见,无任何用户感知 set originalBounds to bounds of tempWindow close tempWindow end if -- 2. 创建目标窗口,加载指定URL make new document with properties {URL:targetURL} -- 3. 调整目标窗口到预设尺寸 set bounds of front window to presetBounds -- 4. 静默恢复Safari默认窗口尺寸,全程不可见 set restoreWindow to make new document with properties {URL:"favorites://"} set visible of restoreWindow to false set bounds of restoreWindow to originalBounds close restoreWindow -- 所有配置完成后,激活目标窗口到前台 activate end tell end run
优化说明
- 全程后台执行所有窗口创建、参数调整操作,仅在最终配置完成后才激活Safari到前台,完全消除原脚本多次开关窗口导致的闪烁
- 所有临时窗口(获取默认尺寸、恢复默认尺寸)均提前设置为
visible: false,不会出现在前台、Dock预览、Mission Control 中,用户完全感知不到操作过程 - 严格满足约束条件:
- 除最终目标窗口外,无任何用户可见的冗余弹窗
- 通过不可见的临时窗口将原有尺寸写回Safari的默认窗口配置,自定义的预设尺寸不会被留存为后续新建窗口的默认值
内容的提问来源于stack exchange,提问作者Skeeve
相关产品推荐
相关产品推荐

