Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot listen to MenuItem's Title changes in the app's MenuBar #38

Open
tomaskupka opened this issue Jan 27, 2023 · 2 comments
Open

Cannot listen to MenuItem's Title changes in the app's MenuBar #38

tomaskupka opened this issue Jan 27, 2023 · 2 comments

Comments

@tomaskupka
Copy link

tomaskupka commented Jan 27, 2023

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.

@soundflix
Copy link

You have to store a reference to your observer.

@tomaskupka
Copy link
Author

tomaskupka commented Jan 28, 2023

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{...}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants