如何在 os x 中以编程方式将桌面背景设置为纯色? [英] How to programmatically set Desktop Background to solid colors in os x?

查看:48
本文介绍了如何在 os x 中以编程方式将桌面背景设置为纯色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式在 os x 桌面中设置纯色作为背景?已找到使用图像以编程方式更改它的方法.

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 - 图像文件 URL,屏幕 - nsscreen 对象,错误 - nserror 对象.

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

推荐答案

fwiw 我经常想将我的 bg 设置为白色,但缺少特定颜色,下面的代码应该可以做到.

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 ...

这篇关于如何在 os x 中以编程方式将桌面背景设置为纯色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆