Get Desktop background image

In a WWDC 2019 "Advances in macOS Security" at 18:40 there is the following code

func getDesktopWindowIds() -> [CGWindowID] {
  let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID)! as! [[String: AnyObject]]
  let DesktopWindowLevel = CGWindowLevelForKey(.desktopWindow)-1
  let DesktopWindows = windows.filter {
    let windowLevel = $0[kCGWindowLayer as String] as! CGWindowLevel
    return windowLevel == desktopWindowLevel
  }
  return desktopWindows.map {
    $0[kCGWindowNumber as String] as! CGWindowID
  }
}

to find the CGWindowID of the Desktop background. This works, but when you then try to get the CGImage for that CGWindowID with

let cgImage = CGWindowListCreateImage(CGRectNull, [.optionIncludingWindow], cgWin, [.bestResolution])

cgImage does get a reference, however it's just a gray image. Not the Desktop picture.

It's clear from the documentation that ScreenCaptureKit should be used.

However, if used I get multiple warnings to the user, the most concerning one states:

"App" would like to record this computer's screen and audio.

This is not true! I do nothing with audio and to say a capture is a recording is also misleading.

Is there way to achieve what use to work before macOS 27? Or a way to change/avoid this misleading warning? Is there a reason why this warning is needed for capturing the Desktop background where before it was explicitly allowed?

Get Desktop background image
 
 
Q