Cannot listen to MenuItem's Title changes in the app's MenuBar
Hey, thanks so much for this library. It works great for observing changes on windows etc.
However I cannot make it work to observe a MenuItem's Title changes in the app's MenuBar. I can construct UIElement object that locates that MenuItem. It's the correct pointer, because I can perform an action successfully.
menuItem.performAction(kAXPressAction)
Is there some trick to achieve that or the app may be even broadcasting those events?
Snippet of my code:
var menuItem = UIElement(getMenuItem()) //my function that returns the MenuItem as AXUIElement
//try menuItem.performAction(kAXPressAction) //just a test if the pointer is working
try observer.addNotification(.titleChanged, forElement: menuItem)
No events are triggered using this code. I tried also Accessibility Inspector and the MenuItem has exact ID, and just changes it's Title (a toggle).
Thanks for any clue.
You have to store a reference to your observer.
Thanks. Could you please elaborate?
I think I do, here is full example:
import Cocoa
import AXSwift
import ApplicationServices
class AppDelegate: NSObject, NSApplicationDelegate {
var observer: Observer!
func applicationDidFinishLaunching(_ aNotification: Notification) {
let app = Application.allForBundleID("com.apple.finder").first!
do {
try startWatcher(app)
} catch let error {
NSLog("Error: Could not watch app [\(app)]: \(error)")
abort()
}
}
func startWatcher(_ app: Application) throws {
observer = app.createObserver { (observer: Observer, element: UIElement, event: AXNotification, info: [String: AnyObject]?) in
var elementDesc: String!
print("Event")
}
var menuItem = UIElement(getMenuItem())
//try menuItem.performAction(kAXPressAction)
try observer.addNotification(.titleChanged, forElement: menuItem)
}
func getMenuItem() -> AXUIElement{...}
}