import Cocoa import ApplicationServices func getFocusedElement() -> AXUIElement? { // 1. System wide let sysWide = AXUIElementCreateSystemWide() var sysFocusedObj: CFTypeRef? if AXUIElementCopyAttributeValue(sysWide, kAXFocusedUIElementAttribute as CFString, &sysFocusedObj) == .success, let obj = sysFocusedObj { return (obj as! AXUIElement) } // 2. Frontmost App if let frontApp = NSWorkspace.shared.frontmostApplication { let appElem = AXUIElementCreateApplication(frontApp.processIdentifier) var appFocusedObj: CFTypeRef? if AXUIElementCopyAttributeValue(appElem, kAXFocusedUIElementAttribute as CFString, &appFocusedObj) == .success, let obj = appFocusedObj { return (obj as! AXUIElement) } } return nil } func inspectElement(_ elem: AXUIElement) { var attrNamesObj: CFArray? AXUIElementCopyAttributeNames(elem, &attrNamesObj) if let names = attrNamesObj as? [String] { print("Attribute Names:", names) for name in names { var val: CFTypeRef? let err = AXUIElementCopyAttributeValue(elem, name as CFString, &val) if err == .success, let val = val { print(" \(name): \(val)") } } } } if let focused = getFocusedElement() { print("Found Focused Element:") inspectElement(focused) } else { print("No focused element found") }