如何使用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"))")
}
}
}
https://stackoverflow.com/questions/48825816
复制相似问题