How to create a customized dock menu for my macOS application in the dock using Xcode
Asked Answered
B

1

6

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

Boomerang answered 24/4, 2020 at 0:7 Comment(3)
Can you confirm that your implementation of applicationDockMenu(_:) is being called? Are other app delegate methods on the same class being called?Suberin
I can confirm that the method was never called as I added a print statement at the top of the method. Running the app in debug mode in Xcode and right-clicked the dock icon and the print statement was never displayed and the breakpoint was never activated. I thought this method should run automatically when the dock icon was clicked on? On the other hand, other app delegate methods like applicationDidFinishLaunching, applicationWillTerminate, or application are being called as expected.Boomerang
Problem solved. I forgot the _ argument in the method call. Code snippet updated. Thanks for your help!Boomerang
S
4

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.

Suberin answered 24/4, 2020 at 0:30 Comment(2)
I think I must not be understanding how to do so correctly. I have decided to take the approach of creating one programmatically without using a separate NIB. But my customized dock item did not show up. Please see my edit in original post above.Boomerang
Same problem here. I tried to follow the documentation and added my own nib file but it did not show.Middlesworth

© 2022 - 2024 — McMap. All rights reserved.