MacOS 添加命令到 Dock 图标

MacOSX Add command to Dock icon

我想知道是否可以通过编程方式将自定义命令添加到 Dock 图标选项(右键单击 Dock 图标时显示的菜单)。

这可能吗?我怎样才能做到这一点?我可以使用 Objective-C,我的目标是 MacOS Mojave

谢谢

有多种方法,一种是在 AppDelegate 中分配 NSMenu in applicationDockMenu:

第二个选项是使用 xib 文件和 Info.plist。这是 Xamarin 的教程,但它在任何框架中都是相似的:https://docs.microsoft.com/en-us/xamarin/mac/user-interface/menu

使用NSApplicationDelegat方法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.