import Cocoa import ApplicationServices func inspectFrontApp() { guard let frontApp = NSWorkspace.shared.frontmostApplication else { return } print("Frontmost App:", frontApp.localizedName ?? "") let appElem = AXUIElementCreateApplication(frontApp.processIdentifier) // 1. Try focused window var windowObj: CFTypeRef? var err = AXUIElementCopyAttributeValue(appElem, kAXFocusedWindowAttribute as CFString, &windowObj) print("kAXFocusedWindowAttribute err:", err.rawValue) if err == .success, let win = windowObj { let winElem = win as! AXUIElement var focusedObj: CFTypeRef? let winErr = AXUIElementCopyAttributeValue(winElem, kAXFocusedUIElementAttribute as CFString, &focusedObj) print("Window focused element err:", winErr.rawValue) if winErr == .success, let elem = focusedObj { let axElem = elem as! AXUIElement var roleObj: CFTypeRef? AXUIElementCopyAttributeValue(axElem, kAXRoleAttribute as CFString, &roleObj) print("Role from window:", roleObj ?? "none") var valObj: CFTypeRef? AXUIElementCopyAttributeValue(axElem, kAXValueAttribute as CFString, &valObj) print("Value from window:", valObj ?? "none") } } } inspectFrontApp()