mdmb
mdmb

Reputation: 5283

Returning NSWindow/ AXUIElement using its pid_t

Is it possible to get a AXUIElementRef of an open application knowing its PID?

My project is opening a file/folder. Then I get its PID and I want to be able to get access to this open window, so that is why I want AXUIElementRef of it.

Upvotes: 1

Views: 607

Answers (2)

ShaoJen Chen
ShaoJen Chen

Reputation: 700

Here is my solution as below in Swift 5

    let applicationRef = AXUIElementCreateApplication(pid)
    
    var uiElementsValue: AnyObject?
    
    AXUIElementCopyAttributeValue(applicationRef, kAXWindowsAttribute as CFString, &uiElementsValue)
    
    guard let appElements = uiElementsValue as? [AXUIElement] else { return }

'appElements' are your opening windows.

Upvotes: 0

Willeke
Willeke

Reputation: 15598

extern AXUIElementRef AXUIElementCreateApplication ( pid_t pid);

Creates and returns the top-level accessibility object for the application with the specified process ID.

AXUIElementCreateApplication

Upvotes: 1

Related Questions