import Cocoa import ApplicationServices func printAXTree(_ elem: AXUIElement, depth: Int = 0) { if depth > 7 { return } let indent = String(repeating: " ", count: depth) var roleObj: CFTypeRef? AXUIElementCopyAttributeValue(elem, kAXRoleAttribute as CFString, &roleObj) let role = (roleObj as? String) ?? "unknown" var valObj: CFTypeRef? AXUIElementCopyAttributeValue(elem, kAXValueAttribute as CFString, &valObj) let val = (valObj as? String) ?? "" var titleObj: CFTypeRef? AXUIElementCopyAttributeValue(elem, kAXTitleAttribute as CFString, &titleObj) let title = (titleObj as? String) ?? "" var focusedObj: CFTypeRef? AXUIElementCopyAttributeValue(elem, kAXFocusedAttribute as CFString, &focusedObj) let isFocused = (focusedObj as? Bool) ?? false print("\(indent)[\(role)] title='\(title)' val='\(val)' focused=\(isFocused)") var childrenObj: CFTypeRef? if AXUIElementCopyAttributeValue(elem, kAXChildrenAttribute as CFString, &childrenObj) == .success, let children = childrenObj as? [AXUIElement] { for child in children { printAXTree(child, depth: depth + 1) } } } if let frontApp = NSWorkspace.shared.frontmostApplication { print("Front App:", frontApp.localizedName ?? "") let appElem = AXUIElementCreateApplication(frontApp.processIdentifier) printAXTree(appElem) }