osascript
osascript是 macOS 自带的命令行 AppleScript 执行器(OS + AppleScript),用于在终端与 macOS 系统服务、GUI 应用交互——弹通知、控制应用、模拟按键、读写剪贴板、查询系统信息。 相当于 macOS 系统自动化的命令行入口,Linux 无对应物。查询应用走 LaunchServices(与open -a同一套机制),不依赖安装路径与应用名大小写。
# 核心概念
# OSA / AppleScript / JXA
- OSA(Open Scripting Architecture):macOS 的自动化架构,支持多种脚本语言
- AppleScript:类英语语法的默认脚本语言,
tell ... to ...、display notification都是它的语法 - JXA(JavaScript for Automation):用 JavaScript 写同样的自动化逻辑,
-l JavaScript切换
# 语法速览(AppleScript)
-- tell 块: 向应用发消息
tell application "Finder"
empty trash
end tell
-- 单行 tell
tell application "Safari" to activate
-- 查询属性(不启动应用, 走 LaunchServices 元数据)
id of app "Klogg" -- "com.github.variar.klogg"
version of app "Safari"
-- 调 shell(在 AppleScript 里)
do shell script "ls -la | head -5"
do shell script "npm -v" with administrator privileges -- sudo, 弹密码框
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 基础用法
osascript -e 'display notification "hi"' # -e: 执行单行语句
osascript -e 'tell app "Finder"' -e 'empty trash' -e 'end tell' # 多个 -e 拼接
osascript -l JavaScript -e 'Application("Finder").name()' # JXA 模式
osascript script.applescript # 执行脚本文件
osascript -f script.applescript # 同上(显式 -f)
1
2
3
4
5
2
3
4
5
heredoc 写多行脚本(推荐,可读性好):
osascript <<'EOF'
tell application "Finder"
set desktops to every desktop
set wallpaper to picture of item 1 of desktops
end tell
EOF
1
2
3
4
5
6
2
3
4
5
6
配套工具:
osacompile -o out.scpt src.applescript # 源码编译为 .scpt
osadecompile out.scpt # .scpt 反编译回源码
1
2
2
# 常用场景速查
# 通知与对话框
# 弹通知(可带声音; 声音: Glass/Hero/Ping/Purr/Sosumi/Submarine/Tink 等)
osascript -e 'display notification "构建完成" with title "CI" sound name "Glass"'
# 对话框(带按钮; 点取消则 exit 非 0, 可用于脚本交互确认)
osascript -e 'display dialog "确认删除?" buttons {"取消", "删除"} default button "删除" cancel button "取消"'
osascript -e 'display dialog "确认?" buttons {"OK"}' && echo "继续执行"
# 带输入框的对话框, 取用户输入
answer=$(osascript -e 'text returned of (display dialog "你的名字?" default answer "")')
# 警告框
osascript -e 'display alert "危险操作" message "不可恢复" as critical'
# 系统文件/目录选择框
osascript -e 'POSIX path of (choose file)'
osascript -e 'POSIX path of (choose folder)'
osascript -e 'choose from list {"a", "b"} with prompt "选一个"'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 剪贴板
osascript -e 'set the clipboard to "hello"' # 写
osascript -e 'the clipboard' # 读(文本)
osascript -e 'the clipboard as «class utf8»' # 读(文本, 指定编码类型)
osascript -e 'clipboard info' # 查剪贴板内容类型
# 注意: 剪贴板为文件/图片等非文本内容时, the clipboard 会报错
# 纯文本读写用 pbcopy/pbpaste 更稳; osascript 优势是能在 AppleScript 脚本内部操作
1
2
3
4
5
6
2
3
4
5
6
# 系统信息与设置
osascript -e 'set volume output volume 50' # 音量 0-100
osascript -e 'output volume of (get volume settings)' # 读音量
osascript -e 'set volume output muted true' # 静音开关
osascript -e 'tell application "Finder" to get bounds of window of desktop' # 屏幕分辨率
1
2
3
4
2
3
4
# 应用控制
osascript -e 'id of app "Klogg"' # bundle id(检测应用是否安装, 不启动)
osascript -e 'version of app "Safari"' # 应用版本
osascript -e 'tell app "Safari" to activate' # 启动并前置
osascript -e 'tell app "Safari" to open location "https://pdd.net"' # 打开 URL
osascript -e 'tell app "Music" to playpause' # 音乐播放/暂停
osascript -e 'tell app "Music" to name of current track' # 当前播放曲目
osascript -e 'tell app "Finder" to empty trash' # 清空废纸篓
osascript -e 'quit app "Safari"' # 退出应用
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# System Events:按键与点击模拟
# 模拟按键(需要辅助功能权限: 系统设置 → 隐私与安全性 → 辅助功能)
osascript -e 'tell application "System Events" to keystroke "h"'
osascript -e 'tell application "System Events" to keystroke "c" using command down' # Cmd+C
osascript -e 'tell application "System Events" to key code 53' # Esc(键码)
1
2
3
4
2
3
4
# 实战示例
# 弹通知(ux notice 同款实现)
osascript -e "display notification \"$1\" with title \"${2:-Notice}\" sound name \"Glass\""
1
# 检测应用是否安装(ux init 同款)
if osascript -e 'id of app "Klogg"' >/dev/null 2>&1; then
echo "已安装"
else
echo "未安装"
fi
1
2
3
4
5
2
3
4
5
# 脚本结束提醒(长任务跑完弹通知)
npm run build && osascript -e 'display notification "build 成功" with title "done"' \
|| osascript -e 'display notification "build 失败" with title "done" sound name "Basso"'
1
2
2
# shell 变量传入 AppleScript(引号转义)
msg="构建完成, 耗时 3 分钟"
osascript -e "display notification \"$msg\" with title \"CI\""
# 注意: 双引号内层用 \" 转义; 变量含双引号会破坏语法, 复杂场景用 heredoc + 环境变量传递
1
2
3
2
3
# 常见问题
# 引号转义(最大坑)
AppleScript 字符串用双引号,嵌在 shell 双引号里需要 \" 转义。变量含引号/反斜杠时会破坏脚本。稳妥方案:
export MSG="含 \"引号\" 的内容"
osascript <<'EOF'
on run
display notification (system attribute "MSG")
end run
EOF
1
2
3
4
5
6
2
3
4
5
6
# 权限
- 自动化权限:首次
tell app "X"控制别的应用时,系统弹授权框(每个应用单独授权) - 辅助功能权限:
keystroke/click需要,必须手动在系统设置里授权,脚本无法自动获得 - 授权位置:系统设置 → 隐私与安全性 → 自动化 / 辅助功能
# 错误处理与退出码
osascript -e 'id of app "NotExist"' ; echo $?
# 执行出错 → exit 1, stderr 有错误信息; 可用于脚本判断
# AppleScript 内部捕获
osascript -e 'try' -e 'id of app "NotExist"' -e 'return "missing"' -e 'on error' -e 'return "not found"' -e 'end try'
1
2
3
4
5
2
3
4
5
# 与 Shortcuts 的关系
macOS 12+ 的「快捷指令」也能命令行执行,复杂自动化可考虑迁移:
shortcuts run "我的快捷指令"
shortcuts list
1
2
2
# 参考链接
上次更新: 2026/10/09, 15:51:11