How to programmatically set Desktop Background to solid colors in os x?

血红的双手。 提交于 2021-01-29 09:18:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!