SHARE
TWEET

Untitled

a guest Feb 21st, 2020 143 in 316 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @implementation NSScreen (DesktopBackgroundImage)
  2.  
  3. // Call this for every NSScreen:
  4. - (NSImage *)desktopBackgroundImage
  5. {
  6.     // The desktop window is drawn by the Dock:
  7.     let dock = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"].firstObject;
  8.     if (dock == nil) {
  9.         return nil;
  10.     }
  11.     let expectedOwnerPID = dock.processIdentifier;
  12.  
  13.     // The desktop window seems to be drawn at this level:
  14.     CGWindowLevel expectedWindowLevel = kCGDesktopWindowLevel - 1;
  15.     CGRect expectedWindowFrame = self.frameForFullscreenWindow;
  16.  
  17.     CFArrayRef windowList = CGWindowListCreate(kCGWindowListOptionOnScreenBelowWindow, kCGNullWindowID);
  18.     NSArray *descriptions = CFBridgingRelease(CGWindowListCreateDescriptionFromArray(windowList));
  19.     CFRelease(windowList);
  20.  
  21.     for (NSDictionary *description in descriptions) {
  22.  
  23.         // Check if the window belongs to the Dock:
  24.         let ownerPID = (pid_t)[[description objectForKey:(__bridge NSString *)kCGWindowOwnerPID] intValue];
  25.         if (ownerPID != expectedOwnerPID) {
  26.             continue;
  27.         }
  28.  
  29.         // Check if the window level matches:
  30.         let windowLevel = (CGWindowLevel)[[description objectForKey:(__bridge NSString *)kCGWindowLayer] intValue];
  31.         if (windowLevel != expectedWindowLevel) {
  32.             continue;
  33.         }
  34.  
  35.         // Check if the window frame matches:
  36.         var windowFrame = CGRectZero;
  37.         let windowFrameDictionary = (NSDictionary *)[description objectForKey:(__bridge NSString *)kCGWindowBounds];
  38.         if (!CGRectMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)windowFrameDictionary, &windowFrame)) {
  39.             continue;
  40.         }
  41.         if (!CGRectEqualToRect(windowFrame, expectedWindowFrame)) {
  42.             continue;
  43.         }
  44.  
  45.         // Get the desktop window's window number:
  46.         let windowNumber = (NSNumber *)[description objectForKey:(__bridge NSString *)kCGWindowNumber];
  47.         if (windowNumber == nil) {
  48.             continue;
  49.         }
  50.  
  51.         // Create the image for the desktop window:
  52.         var desktopImageWindowID = (const void *)windowNumber.unsignedIntegerValue;
  53.         CFArrayRef desktopImageWindowIDList = CFArrayCreate(kCFAllocatorDefault, &desktopImageWindowID, 1, NULL);
  54.         CGImageRef cgImage = CGWindowListCreateImageFromArray(windowFrame, desktopImageWindowIDList, kCGWindowImageDefault);
  55.         NSImage *image = [[NSImage alloc] initWithCGImage:cgImage size:NSMakeSize(CGImageGetWidth(cgImage), CGImageGetHeight(cgImage))];
  56.         CFRelease(desktopImageWindowIDList);
  57.         CFRelease(cgImage);
  58.         return image;
  59.     }
  60.  
  61.     return nil;
  62. }
  63.  
  64. - (NSRect)frameForFullscreenWindow
  65. {
  66.     let zeroScreen = NSScreen.screens.firstObject;
  67.     if (zeroScreen == nil) {
  68.         return NSZeroRect;
  69.     }
  70.  
  71.     var frame = self.frame;
  72.     if (self == zeroScreen) {
  73.         return frame;
  74.     }
  75.  
  76.     frame.origin.y = NSHeight(zeroScreen.frame) - NSHeight(frame) - NSMinY(frame);
  77.     return frame;
  78. }
  79.  
  80. @end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top