在Mac上读取plist
我正在尝试通过读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意,从 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.根据我的本地用户帐户的 plist,以下内容应该适合您:
一个更简单且更易于阅读的解决方案是使用 valueForKeyPath:
According to the plist for my local user account, the following should do for you:
An easier solution, and easier to read, is to use valueForKeyPath:
plist 看起来像这样吗?
如果是这样,该代码应该可以工作。但我猜这些键不在第一个字典的根部?
Does the plist look like this?
If so, that code should work. But I'm guessing the keys aren't at the root of the first dict?
除了现在显示图像之外,我一切正常。使用
我能够获取图像的地址。我现在正在尝试设置此图像的 Windows 背景颜色。在此感谢您的帮助。
I got everything working except for displaying the image now. Using
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.