在Mac上获取桌面背景 [英] Getting desktop background on Mac

查看:261
本文介绍了在Mac上获取桌面背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Mac上获取当前墙纸?只需将我指向API函数即可,以便我可以使用更多Google.

我想我找到了.在 http://lists.apple.com上提到的[NSUserDefaults standardUserDefaults]/archives/student-dev/2004/Aug/msg00140.html

也可以从shell中进行: 默认读取com.apple.desktop背景

从AppleScript中: http://discussions.apple.com/thread.jspa?messageID=7111272

解决方案

更新后的答案(小牛和更高版本)

从Mavericks开始,Apple将桌面图像写入到

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

这是一个SQLite数据库.您可以像这样在终端中打开该文件

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

,然后运行以下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 ;

输出将是

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

UID1是显示器的UID(例如MacBook的显示器,外部显示器等,因为每个显示器可以具有自己的背景图像),UID2是可选的(有时会丢失,这可能是可能的)表示该显示的所有空间),它是一个空间的UID(OS X上的每个显示器可以有多个空间,每个空间可以有自己的背景图像),<PicturePath>是图片的路径(对于该特定空间)在此特定显示器上.)

当然,您可以将应用程序链接到SQLite库,并通过库调用完成所有这些操作,但是当然,如何使用SQLite和SQL语法查询和更新数据答案.一个提示:您通过键入.exit(请注意前导句号!)退出sqlite客户端,然后按Enter键(CTRL + C将不起作用).

仅需注意一点:您可以在应用程序中更新数据库,但这不会生效,因为Dock不会知道它(您可以在其背后进行更改).为了使Dock知道该更改,您已经像killall Dock一样杀死了它,仅将其HUP(killall -HUP Dock)就足够了,但这并不能真正杀死它(我没有测试过).在应用程序中,您必须找到Dock的进程ID并向其发送信号(与killall相同),获取进程ID和发送信号也超出了该答复的范围.

旧版答案(Lion和更早版本)

您处在正确的轨道上.如果您用Carbon/Cocoa编写应用程序,只需加载首选项文件.它位于

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

该词典包含一个键为default的子词典,此子词典包含一个键为ImageFilePath的键,其中包含图像文件的绝对路径.

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

解决方案

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.

这篇关于在Mac上获取桌面背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆