Mac:从终端获取当前桌面图片的路径

6 macos osascript desktop-background

我正在编写一个bash脚本,负责在我的mac上设置桌面背景.我可以设置桌面背景:

$ osascript -e 'tell app "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid White.png"'
Run Code Online (Sandbox Code Playgroud)

但是,我还需要获取桌面图片的路径.我最接近的是:

$ osascript -e 'tell app "Finder" to get desktop picture'
Run Code Online (Sandbox Code Playgroud)

这将返回桌面图片的路径,但是以一种我无法使用的非常奇怪的格式:

document file Solid White.png of folder Solid Colors of folder Desktop Pictures of folder Library of startup disk
Run Code Online (Sandbox Code Playgroud)

有什么方法可以让我回到当前桌面图片的路径:

/Library/Desktop\ Pictures/Solid\ Colors/Solid\ White.png
Run Code Online (Sandbox Code Playgroud)

小智 6

我发现答案可以缩短为一行:

osascript -e 'tell app "finder" to get posix path of (get desktop picture as alias)'
Run Code Online (Sandbox Code Playgroud)

  • 在Sierra上,这给出了一个错误:63:68:执行错误:无法将缺失值转换为类型别名.(-1700) (2认同)

Mar*_*ell 4

像这样:

osascript -e '
    tell application "Finder"
    set theDesktopPic to desktop picture as alias
    set theName to posix path of theDesktopPic
    end tell'

/Users/mark/Documents/Carbon fibre.png
Run Code Online (Sandbox Code Playgroud)