Reading plist on mac

I am trying to set an image from a plist reading. I am working on an application that uses the current desktop wallpaper and sets it as a Windows background image. Files are located. ~/Library/Preferences/com.apple.desktop.plist How to read the key to it? The specific keys I'm trying to read include:

NewImageFilePath
ImageFilePath

I tried this code, similar to reading plist on iPhone, but it did not 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 in the plists line, my plan is to set it to NSImageView or to a method [window setBackgroundColor:[color]];

Thanks in advance.

+3
source share
4 answers

, APIS, 10.6. NSWorkspace, -[NSWorkspace desktopImageOptionsForScreen:] ..

+2

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"];
+1

Does the alloy look like this?

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

If so, this code should work. But I guess the keys are not at the root of the first dict?

0
source

Everything works for me, except for displaying the image now. Using

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

I managed to get the image address. Now I'm trying to set this backgroundColor for windows. Thanks for your help here.

0
source

Source: https://habr.com/ru/post/1789400/


All Articles