XelharK
XelharK

Reputation: 609

Setting the desktop background on all Spaces in Cocoa

I'm writing a small app to change your desktop background. If the user only uses one space, then it's all fine, but when he has multiple spaces the app only works on the currently active space.

I'm using this code

[[NSWorkspace sharedWorkspace] setDesktopImageURL:currentImageURL 
                                        forScreen:screenToChange 
                                          options:screenOptions 
                                            error:&error]

to change the desktop background, and it looks like there's no way to change the background of another space.

I only found answers from several years ago, and nobody asked this specific question. Is there a way to do it in objective-c?

Upvotes: 16

Views: 2342

Answers (2)

Segev
Segev

Reputation: 19303

Although there is no public API for changing spaces background there are ways to do it. The keyword you are looking for is com.apple.desktop.plist which is inside ~/Library/Preferences/ That's the plist that stores all the current background for all the current spaces. If you want to use objective-c you can change this file to your liking or you can use one of the suggested solutions here and here. If you are targeting Mavericks the wallpapers data is here: ~/Library/Application\ Support/Dock/desktoppicture.db"

Upvotes: 6

Hussain Shabbir
Hussain Shabbir

Reputation: 15025

Setting the desktop background on all Spaces in Cocoa

If user wants to set the desktop background for multiple spaces then try the below code.:-

For more information refer this

NSString* path = @"/Users/abc/Desktop/yourImg.png";

NSUserDefaults* def = [NSUserDefaults standardUserDefaults];
NSMutableDictionary* desktopDict = [NSMutableDictionary dictionaryWithDictionary:[def 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:path forKey:@"ImageFilePath"];
        [prefs setObject:path forKey:@"NewImageFilePath"];
        [prefs setObject:@"Never" forKey:@"Change"];
    }];
}];
[def setPersistentDomain:desktopDict forName:@"com.apple.desktop"];

Upvotes: 2

Related Questions