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?
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.
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"
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"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With