Reputation:
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
?
Upvotes: 6
Views: 3444
Reputation: 376
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.
Upvotes: 0
Reputation:
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)'
Upvotes: 6
Reputation: 207758
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
Upvotes: 4