用 osascript 在终端里换桌面壁纸
通过 osascript 控制 Finder 修改壁纸,命令行或脚本一键搞定。
运行 osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/路径/图片.jpg"' 即可更换桌面壁纸。
01命令
命令
$ osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Users/you/Pictures/mountain.jpg"'02作用说明
macOS 没有专门的壁纸命令,但 AppleScript 可以控制 Finder 的 desktop picture 属性,而 osascript 能在终端里运行 AppleScript。很适合写进脚本——比如配合 launchd 每天早上从相册随机挑一张做壁纸。注意图片必须用绝对路径,相对路径会悄悄失效。
03风险
!注意: 终端(或运行脚本的程序)需要“自动化”权限来控制 Finder,macOS 会弹一次授权。此方法会同时更改所有显示器、所有空间的壁纸。
04撤销
撤销
$ osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/System/Library/Desktop Pictures/Sonoma.heic"'05来源
- osascript - execute AppleScript依据: Documents running AppleScript from the shell with osascript -e, including Finder automation.
最后验证: 2026-09-18 · 分类: 媒体