如何使用AppleScript在指定浏览器中批量打开URL列表?
错误根因
你调用的openURLs:withApplicationAt:configuration:completionHandler:是macOS 10.15 Catalina及以上版本才新增的NSWorkspace接口,低于该版本的系统会直接抛出unrecognized selector错误;如果你的系统版本符合要求,报错的原因是你给configuration参数传了missing value,该参数需要传入合法的NSWorkspaceOpenConfiguration实例,不能直接传空值。
解决方案
方案1:修复原代码(仅支持macOS 10.15+)
将最后两行报错的代码替换为以下内容,手动创建默认配置实例传入即可:
-- 创建默认打开配置 set openConfig to current application's NSWorkspaceOpenConfiguration's new() -- 调用接口打开URL sharedWorkspace's openURLs:URLArray withApplicationAt:appURL configuration:openConfig completionHandler:(missing value)
方案2:全版本兼容实现(推荐)
不依赖Foundation桥接接口,直接用AppleScript原生的应用控制逻辑实现,兼容性覆盖所有支持Chrome的macOS版本,写法更简洁稳定:
-- 批量打开的URL列表,可自行添加更多条目 set urlList to {"https://www.google.gr", "https://www.baidu.com"} -- 指定浏览器打开,修改id即可切换其他浏览器:比如Safari是com.apple.Safari,Edge是com.microsoft.Edge tell application id "com.google.Chrome" activate repeat with targetUrl in urlList open location targetUrl end repeat end tell
内容的提问来源于stack exchange,提问作者Robert Kniazidis
相关产品推荐
相关产品推荐

