1

I'm using the following code to set desktop picture:

NSURL* newImage = [[NSURL alloc] initFileURLWithPath:@"/Users/name/Pictures/test.png"];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:newImage forScreen:screen options:nil error:&nserror];

It works fine and the desktop picture changed as I required. But it does not change the system-wide desktop preferences (for example, change picture every 30 minutes). How can I change the system-wide desktop preferences?

Cœur
  • 37,241
  • 25
  • 195
  • 267
nim
  • 384
  • 2
  • 14

1 Answers1

0

Take a look at Preferences and Settings Programming Guide. This should help.

Edit:
Here is the sample code:

NSString* newImgPath = @"/Users/cody/Desktop/stuff/imgs/Black_mac.png";

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary* desktopDict = [NSMutableDictionary dictionaryWithDictionary:[defaults persistentDomainForName:@"com.apple.desktop"]];
NSMutableDictionary* bgDict = [desktopDict objectForKey:@"Background"];
NSMutableDictionary* spaces = [bgDict objectForKey:@"spaces"];
[spaces enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSMutableDictionary* obj, BOOL *stop) {
    [obj enumerateKeysAndObjectsUsingBlock:^(id key, NSMutableDictionary* prefs, BOOL *stop) {
        [prefs setObject:newImgPath forKey:@"ImageFilePath"];
        [prefs setObject:newImgPath forKey:@"NewImageFilePath"];
        [prefs setObject:@"Never" forKey:@"Change"];
    }];
}];

//NSLog(@"%@", desktopDict);

[defaults setPersistentDomain:desktopDict forName:@"com.apple.desktop"];
if ([defaults synchronize] == NO)
    NSLog(@"synchronize failed");

// Restart dock
system ("/usr/bin/killall Dock");
cody
  • 3,233
  • 1
  • 22
  • 25
  • Thanks for the link. But I still have no clue after reading the link. Can you be more specific? – nim Jul 15 '13 at 09:28
  • I found out that there is a commandline defaults that can read/write system preferences (actually it's my user preferences of system applications). For example, defaults read com.apple.desktop. – nim Jul 15 '13 at 10:17
  • Output of defaults read com.apple.desktop: { Background = { spaces = { "" = { 69677376 = { Change = Never; ChangePath = "/Library/Desktop Pictures"; // ... }; "update-id" = 10080000; } – nim Jul 15 '13 at 10:26
  • I do not even know how to write to the Change key because there is a special name "" in its container path. – nim Jul 15 '13 at 10:28
  • Try something like this: `defaults write com.apple.desktop Background '{default = {Change = Never;};}'`. Take a look at ClintonBlackmore's answer for this question: http://stackoverflow.com/questions/431205/how-can-i-programatically-change-the-background-in-mac-os-x. `defaults` utility is using NSUserDefaults class. So everything you can do with `defaults` utility you can do using NSUserDefaults class. – cody Jul 15 '13 at 11:58
  • Just search through the internet, there are lots of examples on how to use this class, you just need to apply those to the 'Change' key. Here is the related question for example: http://stackoverflow.com/questions/9058825/how-do-you-save-a-picture-with-nsuserdefaults – cody Jul 15 '13 at 12:06
  • I think I can do it using AppleScript. Found this excellent article: http://www.macosxautomation.com/applescript/features/system-prefs.html. – nim Jul 15 '13 at 13:33
  • It's all up to you, but programmatically, using Cocoa it can be done with the help of NSUserDefaults. – cody Jul 15 '13 at 15:53
  • Can anyone give me a few lines of sample code of NSUserDefaults as how to modify Desktop Background preferences? – nim Jul 16 '13 at 05:25
  • I've provided the sample code based on the link I've gave you. I've successfully used this code on my machine with 2 desktops – cody Jul 16 '13 at 06:32
  • Thanks for the code! It's great. I'm wondering how do you figure it out all the keys. – nim Jul 16 '13 at 15:06
  • If it helped you - than accept the answer. As for the keys - just print the dictionary (see the commented line) and you'll see all keys. – cody Jul 16 '13 at 15:08