Reputation: 489
I am trying to create a customized dock menu for my swift application in the dock so that when the user right click on the dock tile of my app after it has been launched, it will show my customized item in addition to the default dock menu items. I prefer to do it programmatically or creating the (static) menu item using Xcode.
I have reviewed similar questions posted here (How can I add a menu to the application in the dock?) and (Adding Items to the Dock Menu from my View Controller in my Cocoa App) but both referred to the old nib interface instead of how to create one using storyboard. I also reviewed the file template library in storyboard but couldn't find a template for dock menu (I only see one for Main Menu).
Any pointer on how to achieve this using storyboard or programmatically with swift will be much appreciated.
EDIT 4/24/2020: Since Ken's response, I have decided to customize the dock menu programmatically. Here is how I implemented applicationDockMenu(_:)
in AppDelegate
EDIT #2 4/24/2020: fixed missing _
input argument to method and problem solved.
func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
let myMenu = NSMenu(title: "MyMenu")
let myMenuItem = NSMenuItem(title: "My Item 1", action: nil, keyEquivalent: "")
myMenu.addItem(myMenuItem)
return myMenu
}
I must be missing something else since the new item did not show up with I right-clicked on the app's dock icon after the app is launched.
Any idea?
Thanks.
Kenny
Upvotes: 6
Views: 1737
Reputation: 90551
You can implement the method applicationDockMenu(_:)
in your app delegate class to return a menu of items to be added to the Dock menu.
If you check the docs for that method, it also discusses other methods for providing a static Dock menu. One of them involves creating a separate NIB. If there isn't a template for a menu NIB, you can simply create an empty one and add a menu object to it. That NIB is referenced by a key in the Info.plist file and doesn't need to involve your storyboard.
Upvotes: 4