Skip to content

Instantly share code, notes, and snippets.

@dedeexe
Created March 23, 2023 18:24
Show Gist options
  • Save dedeexe/3cd8ccf760125d692e2eec574269e46d to your computer and use it in GitHub Desktop.
Save dedeexe/3cd8ccf760125d692e2eec574269e46d to your computer and use it in GitHub Desktop.
A function to get the list of windows in MacOS using Swift
func getWindowsList() {
let options = CGWindowListOption(arrayLiteral: .optionOnScreenOnly)
let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(1))
let infoList = windowListInfo as! [[String: Any]]
let windowList = infoList.filter { (($0["kCGWindowLayer"] as? Int) ?? -1) == 0 }
for window in windowList {
print(window["kCGWindowOwnerName"])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment