Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting desktop background on Mac

How do I get the current wallpaper on a Mac? Just point me to an API function so I can Google more.

Edit: I think I found it. [NSUserDefaults standardUserDefaults] mentioned at http://lists.apple.com/archives/student-dev/2004/Aug/msg00140.html

Also possible from shell: defaults read com.apple.desktop Background

And from AppleScript: http://discussions.apple.com/thread.jspa?messageID=7111272

like image 374
Bemmu Avatar asked Nov 19 '08 07:11

Bemmu


People also ask

Where is my desktop background saved Mac?

The Apple delivered desktop images are stored in /Library/Desktop Pictures , so you can eliminate those (or seek those if you used an Apple picture for one or more spaces). Before Lion, the preference file that controls desktop images is text format any to find out the current image, open ~/Library/Preferences/com.

Why has my desktop background disappeared Mac?

Step 1: Click on the Apple logo and go to System Preferences. Step 2: Go to Desktop & Screensaver, which is near the top of this window. Step 3: Near the bottom, you'll see an option called “Change Picture:”. If the box is ticked and looks blue, click on this to untick it.


2 Answers

Updated Answer (Mavericks and newer)

Starting with Mavericks, Apple writes the Desktop images to

/Users/<current-user>/Application Support/Dock/desktoppicture.db 

which is an SQLite database. You can open this file in Terminal like this

sqlite3 "/Application Support/Dock/desktoppicture.db"

and then run the following SELECT:

SELECT display_uuid,space_uuid,value 
FROM preferences 
JOIN data ON preferences.data_id=data.ROWID 
JOIN pictures ON preferences.picture_id=pictures.ROWID
JOIN displays ON pictures.display_id=displays.ROWID 
JOIN spaces ON pictures.space_id=spaces.ROWID ;

The output will be

<UID1>|<UID2>|<PicturePath>
<UID1>|<UID2>|<PicturePath>
:

UID1 is the UID of a display (e.g. the display of your MacBook, an external display, etc. as every display can have an own background image), UID2 is optional (sometimes it is missing, which probably means all spaces of that display) and it is the UID of a space (every display on OS X can have multiple spaces and every space can have an own background iamge) and <PicturePath> is the path to the picture (for this specific space on this specific display).

Of course you can link your App against the SQLite library and do all that with library calls, but how to use SQLite and the SQL syntax for queries and updating data are, of course, way beyond the scope of this answer. Just one tip: You exit the sqlite client by typing .exit (note the leading period!) and hit enter (CTRL+C will not work).

Just one more note: You can update the database in your app, but that will have no effect as the Dock will not know about it (you change it behind its back). To make the Dock aware of that change, you have kill it like killall Dock, it may be enough to just HUP it (killall -HUP Dock), which will not really kill it (I have not tested that). Within an app, you'd have to find the process ID of the Dock and send it a signal (this is the same that killall does), getting process IDs and sending signals is also beyond the scope of that reply.

Legacy Answer (Lion and earlier)

You are on the right track. If you write an application in Carbon/Cocoa, just load the preference file. It is located in

/Users/<current-user>/Library/Preferences/com.apple.desktop.plist

The dictionary contains a sub-dictionary with the key default and this sub dictionary contains a key ImageFilePath, containing the absolute path to the image file.

like image 122
Mecki Avatar answered Sep 28 '22 17:09

Mecki


You can do it in shell + Applescript like this:

#!/bin/bash
osascript -e 'tell app "finder" to get posix path of (get desktop picture as alias)'
like image 24
Mark Setchell Avatar answered Sep 28 '22 15:09

Mark Setchell