Mac: Getting path of current desktop picture from terminal
Asked Answered
E

3

6

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

?

Ecclesia answered 28/2, 2015 at 16:11 Comment(0)
F
4

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
Fanfaronade answered 28/2, 2015 at 16:26 Comment(2)
Does not work on macOS Mojave (Public Beta 5). Gives Can’t make missing value into type alias.Phelps
do you think you can write than in JXA too?Vullo
E
6

I found that the answer can be shortened to one line:

osascript -e 'tell app "finder" to get posix path of (get desktop picture as alias)'
Ecclesia answered 28/2, 2015 at 17:54 Comment(3)
On Sierra, this gives an error: 63:68: execution error: Can’t make missing value into type alias. (-1700)Planchet
On Mojave it gives me 63:68: execution error: Not authorized to send Apple events to Finder. (-1743)Phelps
Works like a charm when executing from iterm with zsh on Mojave.Vowell
F
4

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
Fanfaronade answered 28/2, 2015 at 16:26 Comment(2)
Does not work on macOS Mojave (Public Beta 5). Gives Can’t make missing value into type alias.Phelps
do you think you can write than in JXA too?Vullo
A
0

The following works for me under MacOS Mojave 10.14.6 using both Script Editor and osascript:

tell application "System Events" to get pictures folder of every desktop

Output:

{"/Users/jchilders/Library/Mobile Documents/com~apple~CloudDocs/Wallpapers"}

Replace every with first to get a single entry:

tell application "System Events" to get pictures folder of first desktop

Output:

"/Users/jchilders/Library/Mobile Documents/com~apple~CloudDocs/Wallpapers"

Original answer from apple.stackexchange.com here.

Accost answered 5/9, 2019 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.