Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

4doom4

macrumors newbie
Original poster
Jan 14, 2016
6
2
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:

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()
            }
        }
    }
}
 
I think it's because the windows are created differently as they are run by an Xserver and handed over to macOS' WindowServer as one big bundle when it's all ready. I think you'd need to interact with X11 windows as if it were a regular X desktop and not a macOS WindowServer running them
 
Thanks casperes1996 for this suggestion. This is a little bit what I feared. I have never programmed against X11. Do you have any tutorial or starting point on how to interact we X11 directly through objective-c or swift?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.