Find path of the wallpaper image

Looks like the question of how to get the name and location of the current desktop wallpaper has been plaguing many people for many year. Internet search returned only partial solutions so I hacked out the below solution. I'm hoping that someone will be able to complete the puzzle after me.


Problem statement: Desktop background is set to change picture automatically (e.g. every 5 minutes) using the pictures in Photos. However, there is no easy way to determine where the picture is stored.


Workarounds:

  • On the terminal, run the following command to enable debug message on the wallpaper

    defaults write com.apple.dock desktop-picture-show-debug-text -bool TRUE;killall Dock

  • On mine, it shows something like this

    /Users/username/Library/Caches/com.apple.preference.desktopscreeneffect.desktop/69677504/DSKPhotosRootSource/30595B

  • In my example, the filename "30595B" is an alias and if you cd to the folder and do an ls -l on it you will find the original path underlying this alias. e.g.:

    $ ls -l 30595B

lrwxr-xr-x
1 username
staff
55 25 Apr 10:09
30595B -> /Users/username/Pictures/2011/2011_01_30/IMG_7019.JPG

  • The subfolder name "69677504" appears to be randomly generated by the OS.
  • Each time Desktop changes background, it picks a different alias file from the DSKPhotosRootSource folder. The alias filename is written to the database on ~/Library/Application Support/Dock/desktoppicture.db
  • You can use sqlite3 (on terminal) to query the database. The following query gets the current wallpaper alias name.

    $ /usr/bin/sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db

    sqlite> .headers on

sqlite> SELECT preferences.picture_id, data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id in (1, 2, 3, 4);

picture_id|value

1|30595B

2|30595B

3|30595B

4|30595B

  • Note that there are 4 desktops on the MacBook Pro, so it's possible to have 4 different values from the above query
  • We can automate these steps using AppleScript. I give 2 examples below, one for Finder and the other, for Photos (I have packaged them both as Applications using the Automator so I can quickly click on the applications on the dashboard whenever I see a wallpaper I am interested in)

Example 1, reveal the original file in Finder:

-- Usage: osascript find_wall_pic n

-- n = 1..4 (desktop ID)


on runargv

if (count of argv) < 1 then

set screenId to 1

else

set screenId to item 1 of argv as integer

end if

set a to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id =" & screenId & "\"") as string

set posixaliaspath to "/Users/username/Library/Caches/com.apple.preference.desktopscreeneffect.desktop/69677504/DSKPhotosRootSource/" & a

set aliaspath to (POSIX fileposixaliaspath) as string

set posixpath to POSIX path of aliaspath

set imgfile to POSIX fileposixpath

tell application "Finder"

activate

revealimgfile

end tell

end run

Example 2, reveal the original picture in Photos:

-- Usage: osascript find_wall_pic_in_Photos.scpt n

-- n = 1..4 (desktop ID)


on runargv

if (count of argv) < 1 then

set screenId to 1

else

set screenId to item 1 of argv as integer

end if

set a to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id =" & screenId & "\"") as string

set posixaliaspath to "/Users/username/Library/Caches/com.apple.preference.desktopscreeneffect.desktop/69677504/DSKPhotosRootSource/" & a

tell application "Finder"

set imgFile to (POSIX fileposixaliaspath as alias)

set createDt to creation date of imgFile

set createYear to year of createDt

set createMonth to month of createDt

set imgName to name of imgFile

endtell

tell application "Photos"

set folderName to "Search Results"


activate

set srchkey to imgName & " " & createYear & " " & createMonth as string

set photoList to searchforsrchkey


spotlightitem 1 of photoList


endtell

end run


Question:

Finally, my question is, does anyone know where OSX stores the cache folder name (in my case, the 69677504 folder in the full path /Users/username/Library/Caches/com.apple.preference.desktopscreeneffect.desktop/69677504/DSKPhotosRootSource/), so that we can have a generic solution for finding the actual file of the current wallpaper picture.

iPhone 5s, iOS 11

Posted on Apr 27, 2018 1:38 PM

Reply
Question marked as Top-ranking reply

Posted on Apr 29, 2018 3:50 PM

I think I have found the answer (where Desktop stores the cache location setting). Did a bit more digging around in the desktoppicture database and found
that the cache location stored in preferences table for key=10 and picture_id in (1,2,3,4) for monitor 1; picture_id in (18,21,22) for monitor 2.

The alias file name is stored in data, linked by preferences for key=16, and picture_id in (1,2,3,4) for monitor 1; picture_id in (18,21,22) for monitor 2.

I am guessing the multiple rows are for multiple desktops but the data.value is always the same in my tests for the (1,2,3,4) set and for the (18,21,22) set.

The following query uses just picture_id in (1, 18) to find the alias file location in the cache for monitor 1 and monitor 2 desktops:

/usr/bin/sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db 'select pf1.picture_id, d1.value, d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.picture_id in (1,18) and pf1.key=10 and pf2.key=16;'

Similar questions

7 replies
Question marked as Top-ranking reply

Apr 29, 2018 3:50 PM in response to kangfucius

I think I have found the answer (where Desktop stores the cache location setting). Did a bit more digging around in the desktoppicture database and found
that the cache location stored in preferences table for key=10 and picture_id in (1,2,3,4) for monitor 1; picture_id in (18,21,22) for monitor 2.

The alias file name is stored in data, linked by preferences for key=16, and picture_id in (1,2,3,4) for monitor 1; picture_id in (18,21,22) for monitor 2.

I am guessing the multiple rows are for multiple desktops but the data.value is always the same in my tests for the (1,2,3,4) set and for the (18,21,22) set.

The following query uses just picture_id in (1, 18) to find the alias file location in the cache for monitor 1 and monitor 2 desktops:

/usr/bin/sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db 'select pf1.picture_id, d1.value, d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.picture_id in (1,18) and pf1.key=10 and pf2.key=16;'

Apr 27, 2018 7:40 PM in response to julieda

Thanks julieda. Unfortunately that solution works only on fixed stock images but fails on auto-changing settings. I had also posted the same comments in the thread (yesterday), to use AppleScript to resolve the original image file path hidden in the config db (~/Library/Application Support/Dock/desktoppicture.db)


I am hoping that someone may know where OSX stores the location of the cached images (in my example, ...Library/Caches/com.apple.preference.desktopscreeneffect.desktop/69677504/DSKPhotosRootSource/) So the script can look up instead of hardcoding the location.

Apr 28, 2018 6:18 PM in response to VikingOSX

Thanks @VikingOSX but alas this works only if the Desktop picture were static otherwise it's an elegant solution. If you had set the Desktop picture to change at set intervals then OSX takes a very different path to control and stage the pictures. Not too sure when this all started (after Mavericks?) but OSX now stores the pointer to the current picture in

~/Application Support/Dock/desktoppicture.db

Apr 29, 2018 4:03 PM in response to kangfucius

I have modified the finder script to the generic solution (note I had to convert the file location stored in the db to full path as AppleScript does not like the ~ notation). Similar exercise can be easily replicated for the script for locating the desktop picture in Photos:

-- Usage: osascript find_wall_pic n

-- n = 1 or 2 (monitor ID)


on replace_chars(this_text, search_string, replacement_string)

-- ref: https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html

set AppleScript's text item delimiters to the search_string

set the item_list to every text item of this_text

set AppleScript's text item delimiters to the replacement_string

set this_text to the item_list as string

set AppleScript's text item delimiters to ""

return this_text

end replace_chars


on run argv

if (count of argv) < 1 then

set screenId to 1

else

set screenId to item 1 of argv as integer

end if

if screenId ≤ 1 then

set screenId to 1 -- 1st monior index

else

set screenId to 18 -- 2nd monitor index

end if


-- set a to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id =" & screenId & "\"") as string

-- set posixaliaspath to "/Users/kangfucius/Library/Caches/com.apple.preference.desktopscreeneffect.desk top/69677504/DSKPhotosRootSource/" & a


set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string

set homepath to (do shell script "echo $HOME")

-- replace "~" in the path to actual $HOME dir

set posixaliaspath to replace_chars(posixaliaspath, "~", homepath)


set aliaspath to (POSIX file posixaliaspath) as string

set posixpath to POSIX path of aliaspath

set imgfile to POSIX file posixpath

-- tell application "Finder" to reveal imgfile

tell application "Finder"

activate

reveal imgfile

end tell

end run

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Find path of the wallpaper image

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.