Mac / Cocoa - Getting a list of windows using Accessibility API

后端 未结 4 762
情歌与酒
情歌与酒 2020-11-30 07:02

I want to use the Accessibility API to get a list of all windows for a given application (external).

The goal is to check if a certain window is open. First I chec

4条回答
  •  甜味超标
    2020-11-30 07:55

    This works for me in Swift 5.1:

    let windowList: CFArray? = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID)
    
    for entry in windowList! as Array {
    
        let ownerName: String = entry.object(forKey: kCGWindowName) as? String ?? "N/A"
        let ownerPID: Int = entry.object(forKey: kCGWindowOwnerPID) as? Int ?? 0
        print("ownerName: \(ownerName), ownerPID:\(ownerPID)")
    }
    

提交回复
热议问题