Created
November 22, 2019 04:07
-
-
Save soffes/da6ea98be4f56bc7b8e75079a5224b37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From https://stackoverflow.com/a/58985069/118631 | |
@available(macOS 10.15, *) | |
func canRecordScreen() -> Bool { | |
let runningApplication = NSRunningApplication.current | |
let processIdentifier = runningApplication.processIdentifier | |
guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) | |
as? [[String: AnyObject]] else | |
{ | |
assertionFailure("Invalid window info") | |
return false | |
} | |
for window in windows { | |
// Get information for each window | |
guard let windowProcessIdentifier = (window[String(kCGWindowOwnerPID)] as? Int).flatMap(pid_t.init) else { | |
assertionFailure("Invalid window info") | |
continue | |
} | |
// Don't check windows owned by this process | |
if windowProcessIdentifier == processIdentifier { | |
continue | |
} | |
// Get process information for each window | |
guard let windowRunningApplication = NSRunningApplication(processIdentifier: windowProcessIdentifier) else { | |
// Ignore processes we don't have access to, such as WindowServer, which manages the windows named | |
// "Menubar" and "Backstop Menubar" | |
continue | |
} | |
if window[String(kCGWindowName)] as? String != nil { | |
if windowRunningApplication.executableURL?.lastPathComponent == "Dock" { | |
// Ignore the Dock, which provides the desktop picture | |
continue | |
} else { | |
return true | |
} | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working for me