macosmenuappkit

Appkit - custom action for Settings menu item


In MacOS app I want to do my own custom action when user choses "Settings ..." from app menu, or presses cmd+, . So I need to either modify or replace a corresponding menu item.

How to that? I can access menubar by NSApp.mainMenu, but have now idea how to reliably locate Settings menu item.


Solution

  • So finally I solved it by looking for keyEquivalent. This seems to be more reliable then title.

    
    //main app menu item will always be at position 0, that is reliable
    let appMenu = NSApp.mainMenu!.item(at: 0)!.submenu!
    
    //Settings can be found by keyEquivalent; seems better then by title
    let item = appMenu.items.first(where: {
                $0.keyEquivalent == equiv
            })
    
    //change Settings action as needed
    setsItem.target = MyIntendedTarget
    setsItem.action = #selector(MyIntendedMethod)