如何使用AppleScript唤起位于其他桌面/空间的Chrome窗口
AppleScript跨桌面唤起Chrome窗口解决方案
你之前使用System Events的AXRaise接口仅能枚举当前桌面空间下的进程窗口,无法识别其他空间的窗口,因此只能唤起当前桌面的Chrome窗口。可以直接通过Chrome原生的AppleScript接口实现跨空间窗口唤起,macOS会自动切换到对应窗口所在的桌面。
示例代码
1. 按窗口索引唤起
适合临时快速调用,窗口索引可通过tell application "Google Chrome" to get index of windows查询:
tell application "Google Chrome" -- 示例中索引为2的是Facebook窗口,可替换为你需要的窗口索引 set index of window 2 to 1 activate end tell
2. 按窗口ID唤起(更稳定)
窗口ID是固定唯一值,不会随窗口开闭顺序变化,从你提供的属性输出中可查到对应窗口的id字段:
tell application "Google Chrome" -- 示例中Facebook窗口ID为481,可替换为你需要的窗口ID set targetWindow to window id 481 set index of targetWindow to 1 activate end tell
3. 按窗口名称匹配唤起
适合需要动态查找特定窗口的场景:
tell application "Google Chrome" repeat with currentWindow in windows -- 可替换为你需要匹配的窗口名称关键词 if name of currentWindow contains "Facebook" then set index of currentWindow to 1 activate exit repeat end if end repeat end tell
注意事项
- 运行脚本的工具(脚本编辑器、快捷指令、终端等)需要先在「系统设置→隐私与安全性→辅助功能」中授予权限,否则脚本可能无响应
- 即使开启了调度中心的「根据最近的使用情况自动重新排列空间」功能,上述脚本也可正常运行,无需关联固定的桌面序号
内容的提问来源于stack exchange,提问作者llo
相关产品推荐
相关产品推荐

