问题
How to set a solid color as background in os x desktops programmatically? Have found the way to change it programmatically using the image.
[[NSWorkspace sharedWorkspace]setDesktopImageURL:url forScreen:screen options:nil error:&error];
URL - image file url, screen - nsscreen object, error - nserror object.
Would like to avoid the use of an external resource such as image files. Please help
回答1:
fwiw I've often wanted to set my bg to white but that specific color is missing, the code below should do it.
NSError * err;
NSMutableDictionary < NSWorkspaceDesktopImageOptionKey, id > * m = NSMutableDictionary.dictionary;
m[NSWorkspaceDesktopImageFillColorKey] = NSColor.whiteColor;
m[NSWorkspaceDesktopImageScalingKey] = @(NSImageScaleNone);
// 1x1 png from wikimedia commons
// may have side effects in a pixel appearing somewhere ....
// ... couldn't find it though ...
NSURL * url = [NSBundle.mainBundle URLForResource:@"1x1" withExtension:@"png"];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:url
forScreen:NSScreen.mainScreen
options:m
error:& err];
if ( err )
{
NSLog ( @"Ooops %@", err );
}
You can change the color to taste ...
来源:https://stackoverflow.com/questions/63685678/how-to-programmatically-set-desktop-background-to-solid-colors-in-os-x