I was wondering if somebody figured out how to activate and unhide a macOS window using its kCGWindowNumber. I found out a way to do this using the kCGWindowOwnerPID but since my applications runs in X11 (XQuartz) all their kCGWindowOwnerPID is XQuartz. Therefore, I need to activate a specific window not a specific PID.
I continued playing with code and stackoverflow. I tried playing with the accessibility API and attached to xQuartz through this code:
But none of the windows of xQuartz appear here. If I choose the PID from safari for example I see them. I can bring all windows to the front using:
But not a specific window. Now I am back at where I was with kCGWindowOwnerPID. Each window of each xQuartz application has the same kCGWindowOwnerPID. So the following code doesn't help:
I continued playing with code and stackoverflow. I tried playing with the accessibility API and attached to xQuartz through this code:
Swift:
let xquartzPID:Int32 = 11939
var element = AXUIElementCreateApplication(xquartzPID)
var newarray:CFArray?
AXUIElementCopyAttributeValues(element, kAXWindowsAttribute as CFString, 0, 99999, &newarray)
But none of the windows of xQuartz appear here. If I choose the PID from safari for example I see them. I can bring all windows to the front using:
Swift:
AXUIElementSetAttributeValue(element, kAXFrontmostAttribute as CFString, kCFBooleanTrue)
But not a specific window. Now I am back at where I was with kCGWindowOwnerPID. Each window of each xQuartz application has the same kCGWindowOwnerPID. So the following code doesn't help:
Swift:
func switchToApp(named windowOwnerName: String) {
CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly)
let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
guard let infoList = windowListInfo as NSArray? as? [[String: AnyObject]] else { return }
for entry in infoList {
if let windowName = entry["kCGWindowName"] as? String {
if windowName == windowOwnerName {
let pid = entry["kCGWindowOwnerPID"] as? Int32
let app = NSRunningApplication(processIdentifier: pid!)
app?.activate(options: .activateIgnoringOtherApps)
app?.unhide()
}
}
}
}