Thlbaut
Thlbaut

Reputation: 649

How to get NSAccessibilityChildren

I'm trying to get an array of the children of an AXUIElement with NSAccessibilityChildrenAttribute in order to build a tree of all AXUIElement in an application.

It works fine on Mac native applications but not on java applications (like NetBeans).

When I get an AXUIElement with AXUIElementCopyElementAtPosition (AXUIElementRef application, float x,float y, AXUIElementRef *element), I can retrieve all the parents by going back to the root, but I can't get the children.

Is there an other way to access the children ?

Upvotes: 1

Views: 709

Answers (1)

MongoTheGeek
MongoTheGeek

Reputation: 294

import Foundation
import AppKit
import ApplicationServices

let indd = NSWorkspace.shared.runningApplications.filter{$0.bundleIdentifier == "com.adobe.InDesign"}.first!
indd.activate(options: .activateIgnoringOtherApps)
let app = AXUIElementCreateApplication(indd.processIdentifier)
var attArray:CFArray?
AXUIElementCopyAttributeNames(app, &attArray)
print(attArray ?? "nil")
var childrenPtr:CFTypeRef?
AXUIElementCopyAttributeValue(app, kAXChildrenAttribute as CFString, &childrenPtr)
let children = childrenPtr as! CFArray
print(children)

The CFArray contains the children of the parent application. The same principle can be repeated recursively.

The above code should look similar in Obj-C. I copy pasted this from my workspace.

The key answer to your question is the

AXUIElementCopyAttributeValue(app, kAXChildrenAttribute as CFString, &childrenPtr)

As children are in this model an attribute of the parent.

Upvotes: 3

Related Questions