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