更改在OS X 10.7狮子在所有桌面上的壁纸? [英] Change the wallpaper on all desktops in OS X 10.7 Lion?

查看:194
本文介绍了更改在OS X 10.7狮子在所有桌面上的壁纸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改屏幕上所有桌面(以前称为空格)的壁纸。从OS X 10.6有一个类别,NSWorkspace允许设置壁纸,但是,当我使用此功能只有当前桌面的壁纸更改,所有其他桌面保持不变。

I would like to change the wallpaper of all desktops (formerly "spaces") on a screen. As of OS X 10.6 there is a category to NSWorkspace which allows the setting of the wallpaper, however, when I use this function only the wallpaper of the current desktop gets changed and all the other desktops remain unchanged.

然后我看了桌面偏好plist,并写了一个类,修改它以反映我想要的变化(基本上设置一个新的图像文件路径)。新文件保存后,我发送com.apple.desktopBackgroundChanged通知 - 谷歌,如果你不知道我在说什么,这是人们如何改变前10.6天的壁纸。起初,这没有产生任何结果,所以代替nil作为userInfo字典我发送完全相同的userInfo字典与苹果时,当你更改壁纸在您的设置(订阅在应用程序的通知,并更改壁纸在设置应用程序,你会看到它的外观)。 Luck帮助我在这里,当我发送通知这种方式,由于某种原因Dock崩溃,当它重新加载,它加载设置从偏好文件,因此显示我的更改。

I then looked at the desktop preferences plist and wrote a class that modifies it to reflect the changes I want (basically set a new image file path). After the new file was saved I sent the com.apple.desktop "BackgroundChanged" notification - Google if you don't know what I am talking about, this was how people changed wallpapers in pre 10.6 days. At first this didn't produce any result, so instead of "nil" as userInfo dictionary I sent the exact same userInfo dictionary along as Apple does when you change the wallpaper in your settings (subscribe to the notification in an app and change the wallpaper in the settings app and you will see what it looks like). Luck helped me here, when I sent the notification this way for some reason the Dock crashed and when it reloaded, it loaded the settings from the preferences file thus displaying my changes.

这在10.7.1工作,但是,我会a),而不是有坏的用户体验的码头崩溃和重新加载,和b)使用或多或少保证在未来的版本中工作的路径。利用错误似乎不是一个稳定的路径。

This works on 10.7.1, however, I would a) rather not have the bad user experience of the dock crashing and reloading, and b) use a path that is more or less guaranteed to work in future releases as well. Exploiting a bug doesn't seem like a stable path.

有关如何更改所有桌面壁纸的任何其他想法?我也不确定NSWorkspace壁纸类别的当前行为是目的还是一个错误,但是,从壁纸首选项窗格的行为判断,似乎前者是这种情况。

Any other ideas on how to change the wallpaper of all desktops? I am also unsure whether the current behaviour of the NSWorkspace wallpaper category is intended or a bug, however, judging from the behaviour of the wallpaper preferences pane it seems that the former is the case.

推荐答案

没有api用于设置相同的壁纸到所有屏幕或所有空间,NSWorkspace setDesktopImageURL它被实现为它只设置当前空间的壁纸当前

There is no api for setting the same wallpaper to all screens or all spaces, NSWorkspace setDesktopImageURL it is implemented as such that it only sets the wallpaper for the current space on the current screen, this is how System Preferences does it too.

除了手动修改〜/ Library / Preferences / com.apple.desktop.plist(格式化)的volatile方法之外,改变)和使用通知重新加载它(你经历的崩溃)你可以做的是设置壁纸为空间,当用户切换到它,例如看看NSWorkspaceActiveSpaceDidChangeNotification(如果你的应用程序不总是运行你可以告诉用户切换到所有空间,他想要壁纸应用到),可以说这些方法不是理想的,但至少他们是不稳定的。

Besides the volatile method of manually modifying the ~/Library/Preferences/com.apple.desktop.plist (format could change) and using notifications to reload it (crashes you experienced) what you can do is set the wallpaper to spaces as the user switches to it , e.g. look for NSWorkspaceActiveSpaceDidChangeNotification (if your application is not always running you could tell the user to switch to all spaces he wants the wallpaper to apply to) , arguably these methods are not ideal but at least they are not volatile.

-(void)setWallpaper
{
    NSWorkspace *sws = [NSWorkspace sharedWorkspace];
    NSURL *image = [NSURL fileURLWithPath:@"/Library/Desktop Pictures/Andromeda Galaxy.jpg"];
    NSError *err = nil;
    for (NSScreen *screen in [NSScreen screens]) {
        NSDictionary *opt = [sws desktopImageOptionsForScreen:screen];        
        [sws setDesktopImageURL:image forScreen:screen options:opt error:&err];
        if (err) {
            NSLog(@"%@",[err localizedDescription]);
        }else{
            NSNumber *scr = [[screen deviceDescription] objectForKey:@"NSScreenNumber"];
            NSLog(@"Set %@ for space %i on screen %@",[image path],[self spaceNumber],scr);
        }
    }
}

-(int)spaceNumber
{
    CFArrayRef windowsInSpace = CGWindowListCopyWindowInfo(kCGWindowListOptionAll | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);      
    for (NSMutableDictionary *thisWindow in (NSArray *)windowsInSpace)    {
        if ([thisWindow objectForKey:(id)kCGWindowWorkspace]){
            return [[thisWindow objectForKey:(id)kCGWindowWorkspace] intValue];
        }
    }
    return -1;
}

这篇关于更改在OS X 10.7狮子在所有桌面上的壁纸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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