在Mac上读取plist

发布于 2024-10-15 20:42:04 字数 689 浏览 6 评论 0原文

我正在尝试通过读取 plist 来设置图像。我正在开发一个应用程序,它使用当前的桌面壁纸并将其设置为 Windows 背景图像。这些文件位于~/Library/Preferences/com.apple.desktop.plist 我如何从中读取密钥?我要读取的具体键是;

NewImageFilePath
ImageFilePath

我尝试过这段代码,就像在 iPhone 上读取 plist 一样,但它不起作用。

NSString *plistPath = @"~/Library/Preferences/com.apple.desktop.plist";
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:plistPath] retain];
NSString *item = [plistData objectForKey:@"NewImageFilePath"];

NSLog([NSString stringWithFormat:@"%@", item]);

一旦我可以获得位于 plists 字符串中的实际数据,我的计划是将其设置为 NSImageView 或 [window setBackgroundColor:[color]]; 方法

提前致谢。

I'm trying to set an image from reading a plist. I'm working on an app which uses the current desktop wallpaper and sets it as the windows background image. The files is located ~/Library/Preferences/com.apple.desktop.plist
How do I read a key from it? The specific keys i'm trying to read are;

NewImageFilePath
ImageFilePath

I have tried this code which is like reading a plist on the iPhone but it didnt work.

NSString *plistPath = @"~/Library/Preferences/com.apple.desktop.plist";
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:plistPath] retain];
NSString *item = [plistData objectForKey:@"NewImageFilePath"];

NSLog([NSString stringWithFormat:@"%@", item]);

Once I can get the actual data located in the plists string my plan is to set it to an NSImageView or to the [window setBackgroundColor:[color]]; method

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断爱 2024-10-22 20:42:05

请注意,从 10.6 开始,可以通过公共 APIS 获取有关桌面图像的一些信息。阅读 NSWorkspace 文档,特别是 -[NSWorkspacedesktopImageOptionsForScreen:] 等。

Note that some info on desktop images can be obtained via public APIS, starting from 10.6. Read NSWorkspace documentation, in particular -[NSWorkspace desktopImageOptionsForScreen:] and such.

别理我 2024-10-22 20:42:05

根据我的本地用户帐户的 plist,以下内容应该适合您:

NSString *plistPath = @"~/Library/Preferences/com.apple.desktop.plist";
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSString *item = [[[plistData objectForKey:@"Background"] objectForKey:@"default"] objectForKey:@"NewImageFilePath"];

一个更简单且更易于阅读的解决方案是使用 valueForKeyPath:

NSString *plistPath = @"~/Library/Preferences/com.apple.desktop.plist";
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSString *item = [plistData valueForKeyPath:@"Background.default.NewImageFilePath"];

According to the plist for my local user account, the following should do for you:

NSString *plistPath = @"~/Library/Preferences/com.apple.desktop.plist";
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSString *item = [[[plistData objectForKey:@"Background"] objectForKey:@"default"] objectForKey:@"NewImageFilePath"];

An easier solution, and easier to read, is to use valueForKeyPath:

NSString *plistPath = @"~/Library/Preferences/com.apple.desktop.plist";
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSString *item = [plistData valueForKeyPath:@"Background.default.NewImageFilePath"];
合约呢 2024-10-22 20:42:05

plist 看起来像这样吗?

<dict>
    <key>NewImageFilePath</key>
    <string>...</string>
</dict>

如果是这样,该代码应该可以工作。但我猜这些键不在第一个字典的根部?

Does the plist look like this?

<dict>
    <key>NewImageFilePath</key>
    <string>...</string>
</dict>

If so, that code should work. But I'm guessing the keys aren't at the root of the first dict?

薆情海 2024-10-22 20:42:05

除了现在显示图像之外,我一切正常。使用

NSURL *address = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]];

我能够获取图像的地址。我现在正在尝试设置此图像的 Windows 背景颜色。在此感谢您的帮助。

I got everything working except for displaying the image now. Using

NSURL *address = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]];

I was able to get the address to the image. I now am trying to set the windows backgroundColor this image. Thanks for your help here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文