Skip to content

Instantly share code, notes, and snippets.

@MainasuK
Last active January 26, 2021 06:01
Show Gist options
  • Save MainasuK/4bce9bcb5c99bab7945d85e6ca835fc5 to your computer and use it in GitHub Desktop.
Save MainasuK/4bce9bcb5c99bab7945d85e6ca835fc5 to your computer and use it in GitHub Desktop.
Screen Record Permission Request Helper for macOS Catalina
let stream = CGDisplayStream(display: CGDirectDisplayID(), outputWidth: 1, outputHeight: 1, pixelFormat: Int32(kCVPixelFormatType_32BGRA), properties: nil, handler: { (status, time, surface, update) in
// do nothing
})
let nemu = NSWorkspace().runningApplications.first { application -> Bool in
return application.bundleIdentifier == "com.netease.nemu"
}
let windowInfoArray = CGWindowListCopyWindowInfo(.optionAll, kCGNullWindowID)! as NSArray
let nemuWindowInfo = windowInfoArray.first { anyInfo -> Bool in
guard let info = anyInfo as? NSDictionary else { return false }
guard let pid = info[kCGWindowOwnerPID as NSString] as? NSNumber else { return false }
return pid.int32Value == nemu?.processIdentifier
} as? NSDictionary
let image = nemuWindowInfo
.flatMap { info -> CGWindowID? in
(info[kCGWindowNumber as NSString] as? NSNumber)?.uint32Value
}
.flatMap { windowID -> CGImage? in
CGWindowListCreateImage(.null, .optionIncludingWindow, windowID, [.boundsIgnoreFraming, .nominalResolution])
}
.flatMap { cgImage -> NSImage? in
NSImage(cgImage: cgImage, size: .zero)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment