gpt4 book ai didi

swift - 如何在 Swift 中使用 AXObserverAddNotification?

转载 作者:行者123 更新时间:2023-12-05 06:36:48 30 4
gpt4 key购买 nike

我如何在 Swift 中使用 AXObserverAddNotification 来检测 UI 更改?

Obj C 中有一个很好的答案。

How can my app detect a change to another app's window?

如何将答案翻译成 Swift?

下面的测试代码编译和运行没有错误,但是当我更改主窗口时,callBack 似乎没有被执行。

    func observe(app:pid_t, element:AXUIElement) {
var observer: AXObserver? = nil
func callBack(observer: AXObserver?, element: AXUIElement?, notification: CFString, refcon: UnsafeMutableRawPointer?) {
print("Fired! \(notification)")
}
if AXObserverCreate(app, callBack, &observer) == .success {
print("AXObserverCreate success!")
if AXObserverAddNotification(observer!, element, kAXMainWindowChangedNotification as CFString, UnsafeMutableRawPointer(Unmanaged.passRetained(self).toOpaque())) == .success {
print("AXObserverAddNotification success!")
CFRunLoopAddSource(CFRunLoopGetCurrent(), AXObserverGetRunLoopSource(observer!), .defaultMode)
print("Watching \(element.value(of:"AXTitle"))")
}
}
}

最佳答案

@kyo-ago在他的 project 中封装那些类似 C 的 swift API 方面做得很好.

关于swift - 如何在 Swift 中使用 AXObserverAddNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48825816/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com