applescript可直接控制终端新建窗口、标签页,设置描述文件并运行命令;需通过terminal的applescript词典接口调用do script、create window等命令,且描述文件名须与终端设置中完全一致。
applescript 能直接控制“终端”新建窗口、标签页,设置描述文件、运行命令,实现多开与任务分发——关键在于利用 terminal 的 applescript 词典接口,而非仅靠 shell 命令拼凑。
确认终端支持 AppleScript 并查看可用命令
Terminal 是原生可脚本化应用。验证方法:打开“访达”,进入 /应用程序/实用工具,将“终端”图标拖到“脚本编辑器”图标上。若弹出词典窗口,说明接口就绪,里面列有 do script、create window、set current settings 等核心命令。
- 常用对象包括:
window、tab、settings set - 每个新窗口默认带一个 tab;用
do script可在指定 tab 中执行命令 - 描述文件(Profile)决定外观和行为,必须先存在才能被脚本调用
用 AppleScript 批量新建带不同配置的终端窗口
以下脚本会新建 3 个窗口,分别设为“开发”“日志”“部署”描述文件,并自动运行对应命令:
部署和使用军舰的 macOS Automator 自动化服务集合。包含 5 个实用工作流:PDF转JPG、PNG重命名并转JPG、图像拼接、解压RAR、顺序命名图像文件。一键安装所有服务到 ~/Library/Services/ 目录。使用场景:(1) "安装我的自动化服务",(2) "部署所有 Automato...
use sys : application "System Events" use term : application "Terminal" <p>-- 确保目标描述文件已存在(名称需完全匹配“终端”偏好设置里的标题) set devProfile to "Pro" set logProfile to "Basic" set depProfile to "Homebrew"</p><p>-- 新建窗口并设置描述文件与命令 repeat with i from 1 to 3 if i is 1 then tell term to do script "cd ~/project && clear" in window 1 tell term to set current settings of tab 1 of window 1 to settings set devProfile tell term to set custom title of tab 1 of window 1 to "DEV: project" else if i is 2 then tell term to do script "tail -f /var/log/system.log" in window 1 tell term to set current settings of tab 2 of window 1 to settings set logProfile tell term to set custom title of tab 2 of window 1 to "LOG: system" else tell term to do script "cd ~/deploy && ./sync.sh" in window 1 tell term to set current settings of tab 3 of window 1 to settings set depProfile tell term to set custom title of tab 3 of window 1 to "DEPLOY" end if end repeat</p>
- 脚本中
in window 1表示在首个窗口内新增 tab;也可用create window单独建多个独立窗口 - 描述文件名必须与“终端”>“设置”中显示的名称一致(区分大小写)
-
custom title可让窗口标题清晰标识用途,配合不同背景色更易识别
从终端命令行触发 AppleScript 多开任务
把上述逻辑封装成 .scpt 文件(如 open-term-tasks.scpt),就能用 osascript 直接调用:
- 保存脚本后,在终端中运行:
osascript ~/Scripts/open-term-tasks.scpt - 可配合 launchd 实现开机自动拉起多窗口环境
- 也可嵌入 Automator 工作流程,绑定快捷键或菜单栏操作
- 若需传参(比如动态指定项目路径),可在脚本中读取
argv,调用时加参数:osascript open-term-tasks.scpt "~/myapp"
搭配描述文件提升多开实用性
单纯多开没意义,关键靠描述文件做区分:
- 在“终端”>“设置”中复制一份“基本”描述文件,改名为“DEV”,设蓝底白字+自定义字体
- 再建“LOG”描述文件,用灰底绿字+禁用回滚(减少内存占用)
- 所有描述文件可导出为 .terminal 文件备份,便于迁移或团队共享
- 脚本里通过
settings set "DEV"绑定,窗口一打开就自带风格和行为










