Get current Cocoa wallpapers

I use this code to get the current wallpaper:

NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]]; 

This works fine, but when I set the image folder as wallpaper (as shown in the picture), imageURL is a directory, so how can I get the current USURL wallpaper in this situation?

enter image description here

+8
cocoa wallpaper
source share
2 answers

I tried to do the same. You can get the current URL of the desktop image:

  • Getting the current UUID of the space from the com.apple.spaces property list,
  • Search for the list of com.apple.desktop properties for the corresponding space,
  • Retrieving URLs from LastName Property

I am still working on this, but the following code gets the correct URL ... sometimes. The main problem is that property lists are not updated often enough, and I could not get them to be updated (without waiting for the dock). Let me know if you come up with something!

 NSDictionary *spacesPLIST = (__bridge NSDictionary *)(CFPreferencesCopyAppValue(CFSTR("SpacesConfiguration"), CFSTR("com.apple.spaces"))); NSDictionary *desktopPLIST = (__bridge NSDictionary *)(CFPreferencesCopyAppValue(CFSTR("Background"), CFSTR("com.apple.desktop"))); NSArray *monitors = [spacesPLIST valueForKeyPath:@"Management Data.Monitors"]; NSInteger monitorIndex = 0; if ([monitors count] > 1) { //search for main (or ask user to select) } NSDictionary *monitor = [monitors objectAtIndex:monitorIndex]; NSDictionary *spaces = [desktopPLIST valueForKey:@"spaces"]; NSString *currentSpaceUUID = [monitor valueForKeyPath:@"Current Space.uuid"]; NSDictionary *currentSpace = [spaces valueForKey:currentSpaceUUID]; NSURL *desktopPicturesDirectory = [NSURL fileURLWithPath:[currentSpace valueForKeyPath:@"default.ChangePath"] isDirectory:true]; NSString *desktopPictureName = [currentSpace valueForKeyPath:@"default.LastName"]; NSURL *desktopPictureURL = [NSURL URLWithString:desktopPictureName relativeToURL:desktopPicturesDirectory]; [[NSWorkspace sharedWorkspace] selectFile:[desktopPictureURL path] inFileViewerRootedAtPath:@""]; 
+3
source share

There is another way to get the image by taking a screenshot from the current wallpaper.

 extension NSImage { static func desktopPicture() -> NSImage { let windows = CGWindowListCopyWindowInfo( CGWindowListOption.OptionOnScreenOnly, CGWindowID(0))! as NSArray var index = 0 for var i = 0; i < windows.count; i++ { let window = windows[i] // we need windows owned by Dock let owner = window["kCGWindowOwnerName"] as! String if owner != "Dock" { continue } // we need windows named like "Desktop Picture %" let name = window["kCGWindowName"] as! String if !name.hasPrefix("Desktop Picture") { continue } // wee need the one which belongs to the current screen let bounds = window["kCGWindowBounds"] as! NSDictionary let x = bounds["X"] as! CGFloat if x == NSScreen.mainScreen()!.frame.origin.x { index = window["kCGWindowNumber"] as! Int break } } let cgImage = CGWindowListCreateImage( CGRectZero, CGWindowListOption(arrayLiteral: CGWindowListOption.OptionIncludingWindow), CGWindowID(index), CGWindowImageOption.Default)! let image = NSImage(CGImage: cgImage, size: NSScreen.mainScreen()!.frame.size) return image } } 

This approach looks much easier IMHO if you need a picture, not a URL.

Please note that wallpapers are no longer defined in pl. com.apple.dektop: starting with Mavericks, the parameter moves to ~ / Library / Application Support / Dock / desktoppicture.db. This is an SQLite file, and the "data" table contains the URL.

+2
source share

All Articles