RuLoViC
RuLoViC

Reputation: 855

MacOSX Add command to Dock icon

I would like to know if it is possible to add custom commands to Dock icon options (menu show when right-clicking on Dock icon) programatically.

Is that possible? How could I achieve that? I can use Objective-C and I am targetting MacOS Mojave

Thanks

Upvotes: 1

Views: 393

Answers (2)

Willeke
Willeke

Reputation: 15589

Use NSApplicationDelegat method applicationDockMenu(_:):

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

Allows the delegate to supply a dock menu for the application dynamically.

Discussion

You can also connect a menu in Interface Builder to the dockMenu outlet. A third way for your application to specify a dock menu is to provide an NSMenu in a nib.

If this method returns a menu, this menu takes precedence over the dockMenu in the nib.

The target and action for each menu item are passed to the dock. On selection of the menu item the dock messages your application, which should invoke [NSApp sendAction:selector to:target from:nil].

To specify an NSMenu in a nib, you add the nib name to the info.plist, using the key AppleDockMenu. The nib name is specified without an extension. You then create a connection from the file’s owner object (which by default is NSApplication) to the menu. Connect the menu to the dockMenu outlet of NSApplication. The menu is in its own nib file so it can be loaded lazily when the dockMenu is requested, rather than at launch time.

Upvotes: 1

Ivan I
Ivan I

Reputation: 9990

There are multiple ways, one would be to assign NSMenu in applicationDockMenu: in AppDelegate.

The second option is to use xib file and Info.plist. This is Xamarin's tutorial for that, but it is similar in any framework: https://learn.microsoft.com/en-us/xamarin/mac/user-interface/menu

Upvotes: 1

Related Questions