获取Safari前台URL并在Chrome无痕打开的AppleScript实现方案
实现Safari前台链接打开至Chrome无痕窗口的AppleScript方案
核心实现代码
以下是可直接运行的完整脚本,整合了Safari URL提取、Chrome无痕窗口调用逻辑,自带基础容错:
-- 获取Safari前台激活标签页的URL tell application "Safari" -- 无前台文档时直接退出脚本,避免报错 if not (exists front document) then return set safariCurrentURL to URL of front document end tell -- 调用Google Chrome无痕窗口加载目标URL tell application "Google Chrome" activate -- 优先复用已打开的无痕窗口,无则新建 set targetIncognitoWindow to missing value repeat with browserWindow in windows if mode of browserWindow is "incognito" then set targetIncognitoWindow to browserWindow exit repeat end if end repeat if targetIncognitoWindow is missing value then set targetIncognitoWindow to make new window with properties {mode:"incognito"} end if -- 在目标无痕窗口中打开链接 tell targetIncognitoWindow make new tab with properties {URL:safariCurrentURL} end tell end tell
关键逻辑说明
- Chrome无痕窗口核心调用:原生AppleScript中通过创建窗口时指定
mode:"incognito"属性,即可直接生成无痕窗口,无需调用shell命令。 - 容错处理:新增了Safari前台文档存在性判断,避免Safari未启动、无打开页面时脚本崩溃。
- 窗口复用逻辑:默认优先在已打开的Chrome无痕窗口中新增标签,若需每次都新建独立无痕窗口,可删除遍历窗口的逻辑,直接新建带
mode:"incognito"属性的窗口即可。
注意事项
- 首次运行脚本时,系统会弹出自动化权限请求,需允许脚本控制Safari与Google Chrome,否则无法读取URL、调用Chrome。
- 若需要获取Safari前台标签页的标题(即你提到的
name of front document),只需在Safari的tell块中新增一行set safariPageName to name of front document即可,后续可按需调用该变量。
内容的提问来源于stack exchange,提问作者BhishanPoudel
相关产品推荐
相关产品推荐

