Enabling Screen-Recording API in Catalina (kCGWindowName)
Asked Answered
I

2

6

The following code worked fine in Mojave .

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly, CGWindowListOption.optionOnScreenAboveWindow)
let windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]

for entry  in windowList!
{ let name = (entry[kCGWindowName as String] != nil) ? entry[kCGWindowName as String] as! String : ""
  ...

In Catalina

entry[kCGWindowName as String] 

always is nil

In SO: Detecting screen recording settings on macOS Catalina I read, that is´t required to enable the "screen recording API" an how to detect, if it's enabled.

Unfortunately I do not find out, how to enable the "screen recording API".

As mentioned in SO: macOS Catalina screen recording permission I switched on Automatic Code Signing.

In the System Preferences I see no "+" to add an App, to grant "screen recording".

How can the API Permission for Screen Recording be granted?

Ideate answered 14/12, 2019 at 16:29 Comment(3)
My situation is different, as I'm using javascript via nodejs, but I feel it's relevant. I'm experiencing the same issue with null window names. You can check out the code here: github.com/karaggeorge/mac-screen-capture-permissions, which ultimately enabled me to request screen recording permissions in catalina. My app now has screen recording permissions, but still cannot get window names.Ioves
I take it back! I had an unrelated bug. I can confirm that getting window names works with permission and fails without it.Ioves
developer.apple.com/videos/play/wwdc2019/701/?time=930Langton
P
5

This a low level api to request access to screen recording. Calling this functions will present a prompt to give access for screen recording.

/* Requests event listening access if absent, potentially prompting */
@available(macOS 10.15, *)
public func CGRequestScreenCaptureAccess() -> Bool
Parabasis answered 3/1, 2021 at 18:1 Comment(1)
Please note @available(macOS 10.15, *) is not correct. It's crashing on Catalina. Available on Big Sur onlyFrenzy
M
2

You need to call any Screen Record API function. For example screenshot:

(void)showScreenRecordingPrompt {
  /*
   macos 10.14 and lower do not require screen recording permission 
   to get window titles 
  */
  if(@available(macos 10.15, *)) {
    /*
     To minimize the intrusion just make a 1px image of the upper left corner
     This way there is no real possibilty to access any private data
    */
    CGImageRef screenshot = CGWindowListCreateImage(
        CGRectMake(0, 0, 1, 1),
        kCGWindowListOptionOnScreenOnly,
        kCGNullWindowID,
        kCGWindowImageDefault);

    CFRelease(screenshot);

}

The solution was get from here: https://blog.csdn.net/lovechris00/article/details/96979960

Mythological answered 20/3, 2020 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.