bibble triple
bibble triple

Reputation: 67

adding menu items to the right click app menu in the dock casues them to be re-added each time i click

so i've been trying to add items to the right click menu when u right click on the app icon in the dock in macos

but when i do it with this code whenver i right click the app icon it re-adds the menu item

 class myclass: NSObject , NSApplicationDelegate{


   func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {


    dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent:     "")

    return dockMenu
   }

enter image description here

Upvotes: 1

Views: 945

Answers (1)

vadian
vadian

Reputation: 285290

You have to create the NSMenu inside the method

func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {

    let dockMenu = NSMenu()
    dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent:     "")
    return dockMenu
}

or remove the items

let dockMenu = NSMenu()

func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {

    dockMenu.removeAllItems()
    dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent:     "")
    return dockMenu
}

However I would declare it in Interface Builder and use an outlet.

Upvotes: 0

Related Questions