Search code examples
macososascriptdesktop-background

Mac: Getting path of current desktop picture from terminal


I'm writing a bash script that takes care of setting the desktop background on my mac. I can set the desktop background with:

$ osascript -e 'tell app "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid White.png"'

However, I also need to GET the path of the desktop picture. The closest I've gotten is:

$ osascript -e 'tell app "Finder" to get desktop picture'

This returns the path of the desktop picture but in a really weird format that I can't use:

document file Solid White.png of folder Solid Colors of folder Desktop Pictures of folder Library of startup disk

Is there any way I can get the path of the current desktop picture that would return:

/Library/Desktop\ Pictures/Solid\ Colors/Solid\ White.png

?


Solution

  • Like this:

    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