Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 335
XelharK Avatar asked Jan 29 '13 15:01

XelharK


People also ask

How do you make all your desktop backgrounds the same on a Mac?

Right-click on the Dock icon for System Preferences and select Options > [Assign To] All Desktops. Select your desired image for the current desktop from System Preferences.


2 Answers

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"

like image 141
Segev Avatar answered Oct 06 '22 03:10

Segev


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"];
like image 21
Hussain Shabbir Avatar answered Oct 06 '22 01:10

Hussain Shabbir