在Cocoa中的所有空间上设置桌面背景 [英] Setting the desktop background on all Spaces in Cocoa

查看:198
本文介绍了在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.

我使用这个代码

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

我只找到几年前的答案,没有人问这个具体问题。有没有办法在objective-c里做?

I only found answers from several years ago, and nobody asked this specific question. Is there a way to do it in objective-c?

推荐答案

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

有关详情,请访问请参阅此

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"];

这篇关于在Cocoa中的所有空间上设置桌面背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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