0

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

a.masri
  • 2,439
  • 1
  • 14
  • 32

1 Answers1

1

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.

vadian
  • 274,689
  • 30
  • 353
  • 361