Getting desktop background on Mac

2020-06-05 01:34发布

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

2条回答
该账号已被封号
2楼-- · 2020-06-05 01:39

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-06-05 01:41

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)'
查看更多
登录 后发表回答