@@ -167,11 +167,22 @@ func takesText(_ element: AXUIElement) -> Bool {
167167 return AXUIElementIsAttributeSettable ( element , kAXSelectedTextRangeAttribute as CFString , & settable ) == . success && settable . boolValue
168168}
169169/// The window holding `element`: the element itself when it is a window or sheet, which report no
170- /// window of their own, otherwise the window it reports.
170+ /// window of their own, otherwise the window it reports. Controls of an open or save panel belong to
171+ /// the panel's service process and report no window, so for them the nearest window or sheet among
172+ /// their ancestors is used.
171173func containingWindow ( _ element : AXUIElement ) -> AXUIElement ? {
172- if [ kAXWindowRole , kAXSheetRole ] . contains ( attribute ( element , kAXRoleAttribute ) as? String ?? " " ) { return element }
173- guard let raw = attribute ( element , kAXWindowAttribute ) , CFGetTypeID ( raw ) == AXUIElementGetTypeID ( ) else { return nil }
174- return unsafeBitCast ( raw , to : AXUIElement . self )
174+ let windowRoles = [ kAXWindowRole , kAXSheetRole ]
175+ if windowRoles . contains ( attribute ( element , kAXRoleAttribute ) as? String ?? " " ) { return element }
176+ if let raw = attribute ( element , kAXWindowAttribute ) , CFGetTypeID ( raw ) == AXUIElementGetTypeID ( ) {
177+ return unsafeBitCast ( raw , to : AXUIElement . self )
178+ }
179+ var current = element
180+ for _ in 0 ..< 64 {
181+ guard let raw = attribute ( current , kAXParentAttribute ) , CFGetTypeID ( raw ) == AXUIElementGetTypeID ( ) else { return nil }
182+ current = unsafeBitCast ( raw , to : AXUIElement . self )
183+ if windowRoles . contains ( attribute ( current , kAXRoleAttribute ) as? String ?? " " ) { return current }
184+ }
185+ return nil
175186}
176187func axActions ( _ element : AXUIElement ) -> [ String ] {
177188 var names : CFArray ?
0 commit comments