如何用AppleScript/Shell修改macOS所有空间的壁纸?
如何通过AppleScript/Shell修改macOS所有空间的壁纸?
你提到的热门AppleScript脚本仅能修改当前活跃空间的壁纸,原因是在部分macOS版本(如Ventura及以后)中,System Events的every desktop并不会真正遍历所有空间,仅指向当前桌面。以下是两种可行的解决方案:
AppleScript 解决方案
这个方法通过显式遍历所有桌面对象并设置壁纸,同时需要确保运行脚本的应用拥有足够权限:
tell application "System Events" set allDesktops to every desktop repeat with targetDesktop in allDesktops set picture of targetDesktop to "/Users/xyz/Documents/Screenshots/wallpaper.png" end repeat end tell
权限说明
前往「系统设置」>「隐私与安全性」>「自动化」,给运行脚本的应用(如Script Editor、Terminal)勾选允许控制System Events。
Shell 解决方案
通过直接修改系统偏好设置文件并重启Dock,这种方法更稳定,不受桌面空间活跃状态影响:
# 设置所有空间的静态壁纸路径 defaults write com.apple.desktop Background '{default = {ImageFilePath = "/Users/xyz/Documents/Screenshots/wallpaper.png";};}' # 重启Dock使设置生效 killall Dock
注意事项
- 路径必须写绝对路径,确保壁纸文件存在;
- 如果使用动态壁纸或HEIC格式,可添加
ImageStyle参数调整,例如ImageStyle = "Centered";; killall Dock会短暂重启Dock进程,打开的窗口会临时闪烁,属于正常现象。
内容的提问来源于stack exchange,提问作者user4150758
相关产品推荐
相关产品推荐

