【问题标题】:How Get UI Elements of a Window? Swift如何获取窗口的 UI 元素?迅速
【发布时间】:2021-10-30 07:17:50
【问题描述】:

如何将此 AppleScript 代码翻译成 Swift ?

tell application "System Events" to tell process "Safari" to get UI elements of first window

我已经到达“Safari”的第一个窗口,但我不知道如何获取 UI 元素

let pid = NSWorkspace.shared.runningApplications.first(where: {$0.localizedName == "Safari"})?.processIdentifier
let appRef = AXUIElementCreateApplication(pid!)
var windows: AnyObject?
_ = AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute as CFString, &windows)
if let firstWindow = (windows as? [AXUIElement])?.first{
    print(firstWindow)
}

【问题讨论】:

    标签: swift cocoa applescript


    【解决方案1】:

    您可以使用相同的AXUIElementCopyAttributeValue()查询窗口的孩子,以及孩子的孩子,等等。

    我自己喜欢在现有类型上添加扩展,如果可能的话,为了更清晰:

    extension AXUIElement {
        var children: [AXUIElement]? {
            var childrenPtr: CFTypeRef?
            AXUIElementCopyAttributeValue(appRef, kAXChildrenAttribute as CFString, &childrenPtr)
            return childrenPtr as? [AXUIElement]
        }
    }
    

    然后您可以在代码中使用它:

    if let firstWindow = (windows as? [AXUIElement])?.first{
        print(firstWindow, firstWindow.children)
    }
    

    您可以更进一步,通过向扩展添加更多功能来简化AXUIElement 消费者代码:

    extension AXUIElement {
        
        static func from(pid: pid_t) -> AXUIElement { AXUIElementCreateApplication(pid) }
        
        var windows: [AXUIElement]? { value(forAttribute: kAXWindowsAttribute) }
        
        var children: [AXUIElement]? { value(forAttribute: kAXChildrenAttribute) }
        
        func value<T>(forAttribute attribute: String) -> T? {
            var attributeValue: CFTypeRef?
            AXUIElementCopyAttributeValue(self, attribute as CFString, &attributeValue)
            return attributeValue as? T
        }
    }
    
    let pid = ...
    let app = AXUIElement.from(pid: pid!)
    if let firstWindow = app.windows?.first{
        print(firstWindow, firstWindow.children)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      相关资源
      最近更新 更多