NSMenu - anyone?

I am attempting to create a NSMenu (mainmenu) and most everything works out of the box…


I cannot get them to become enabled

“New” and “Open” call selectors in NSDocumentController, and the others in NSDocument.

Well I am not using NSDocument… (and the document on that class in crap at best)

Anyways… I created a test NSMenu that calls a custom protocol

@objc protocol myFileActions {
    func saveDocument(_ sender:AnyObject)

and the NSMenuItem is

let menuSAVE = NSMenuItem(title: "Save", action: #selector(myFileActions.saveDocument(_:)), keyEquivalent: "s")

It shows up in the main menu, but its grayed out

I’ve tried various combinations of this

   menuSAVE.isEnabled = true
        NSApp.mainMenu!.autoenablesItems = false
        NSApp.mainMenu!.item(withTitle: "Save")?.isEnabled = true


Note : I need similar menu function for custom actions that are not even file related

Solved (I think)

Instead of adding the selector in the menu definition
I overrode and added this in the class where the response was required…

menuSAVE.action = #selector(saveDocument(_:))


  @objc func (saveDocument(_ sender:Any?) { print("(saveDocument here") }

what is strange is isENABLED=false does not gray itout, I have to set ACTION=NIL

Your solution is somehow like the EnableMenuItems event of Xojo, if I get it right (in that you have to tell the item to be enabled at a given moment, synchronously).

It is… just not sure why isEnabled doesn’t work and I have to set the acton to NIL

Perhaps like in Xojo where if you have a menu handler, the item becomes available as long as you don’t set AutoEnable to false?
Still, disabled the menu item in Xojo works, but I’d guess the behaviour you’re seeing is along those lines…

Did you read these remarks? Guess it‘s a protocol thing.
This property has no effect unless the menu in which the item will be added or is already a part of has been sent setAutoenablesItems:NO. If a menu item is disabled, its keyboard equivalent is also disabled. See the NSMenuValidation informal protocol specification for cautions regarding this method.