Getting desktop background on Mac

前端 未结 2 1739
清酒与你
清酒与你 2021-02-10 07:12

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] menti

2条回答
  •  时光说笑
    2021-02-10 07:24

    Updated Answer (Mavericks and newer)

    Starting with Mavericks, Apple writes the Desktop images to

    /Users//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 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 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//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.

提交回复
热议问题