Change the wallpaper on all desktops in OS X 10.7 Lion?

前端 未结 1 1759
栀梦
栀梦 2020-12-31 05:08

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 wa

相关标签:
1条回答
  • 2020-12-31 05:14

    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.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题