How to programmatically get the path to the wallpaper on the active desktop

applescriptdesktopdockterminal

I'm trying to get the path to the wallpaper in the active desktop.
However, I'm having trouble reliably getting this.

The following sources indicate that ~/Library/Application Support/Dock/desktoppicture.db is a sqlite database
with information about the background.

However, these sources set a new background and don't try
to read the contents of the sqlite database for the active desktop.

I've dumped the contents of this file on my system and have uploaded
it as a gist at https://gist.github.com/bamos/6c5fb63ec1d9b8dc9205.

This file seems to contain information about displays,
and reading the last value of data seems to get the active wallpaper
sometimes.

  • Is there a way I can use desktoppicture.db to get the wallpaper in the active desktop every time?
  • Is there another way I can programmatically get a path to the active wallpaper?

Edit: forums.macrumors.com/showthread.php?t=1525299 (can somebody add http?) is another
source of getting this information with AppleScript, but for some reason
the following snippet is returning Wave.jpg when Wave.jpg isn't the
active wallpaper.

tell application "Finder"
    set theDesktopPic to desktop picture
    set theName to displayed name of theDesktopPic
    display dialog theName
end tell

To clarify, my wallpaper is set to randomly cycle through
a directory.


I tried running the following command as suggested in the answers.

tell application "System Events" to get properties of every desktop

However, on my system, this outputs:

display name:Color LCD, change interval:1800.0, id:69731840, random order:true, picture rotation:1, pictures folder:/Users/bamos/Pictures/wallpaper/nature, picture:/Library/Desktop Pictures/Wave.jpg, translucent menu bar:missing value, class:desktop,

which doesn't include the wallpaper of the active desktop.

I disabled the random picture cycling and

osascript -e 'tell application "Finder" to set desktop picture to {"Macintosh HD:Library:Desktop Pictures:Beach.jpg"} as alias'

changes my background to the stock beach wallpaper, and

osascript -e 'tell application "System Events" to get properties of every desktop'

now shows picture:/Library/Desktop Pictures/Beach.jpg. However, this is not helpful because I want to get the background when I'm randomly cycling through a directory.

Best Answer

tell application "System Events" to picture of desktop 1

View all properties:

tell application "System Events" to get properties of every desktop