Show dock menu shortcuts in OS X?

My question is pretty simple:

To use the custom menu for the application icon on the dock, - (NSMenu*) applicationDockMenu: (id) sender; NSApplicationDelegate should return a menu that will be displayed by the dock.

Using setImage on NSMenuItem , you can normally add icons to the menu. They appear in the normal menu, but not in the context menu of the dock application icon.

Then how did Apple manage QuickTime, Xcode, Preview to show icons in the list of recently opened files available in their dock menu?

thanks.

+6
source share
2 answers

The list of recent files is actually part of the standard Dock icon menu. To use it in your application, you must create an application based on NSDocument . Using NSDocument , you will get a free menu / file behavior.

If your application cannot be based on NSDocument , you can tell Cocoa to keep a list of recent documents based on URLs:

 NSDocumentController *docController = [NSDocumentController sharedDocumentController]; [docController noteNewRecentDocumentURL:locationOfMyRecentFile1]; [docController noteNewRecentDocumentURL:locationOfMyRecentFile2]; [docController noteNewRecentDocumentURL:locationOfMyRecentFile3]; 

Note that currently -noteNewRecentDocumentURL: only supports the URL file:// (which you can create from the path with +[NSURL fileURLWithPath:] .) In the future, its behavior will apparently change to allow URLs with other schemes.

+3
source

Here's my understanding, which is partly conjectural and related to implementation details:

Dock runs in a separate process, and you cannot pass arbitrary NSImage trivially across the process border from your application to the Dock. There are only two types of images that can be transferred properly: standard system icons and icons in your resource pack. But I do not think that NSImage does the necessary spells for any of them.

So you have to use Carbon. In particular, you need to use SetMenuItemIconHandle with kMenuSystemIconSelectorType (covers Carbon IconRef s obtained with GetIconRef ) or kMenuIconResourceType ( CFString , which refer to the .icns file in the Resources folder of your application).

The corresponding headers are <HIToolbox/MacApplication.h> (for GetApplicationDockTileMenu ), <HIToolbox/Menus.h> (for SetMenuItemIconHandle ) and <HIServices/Icons.h> , (for GetIconRef if you use system icons).

Unconfirmed, but it should look something like this:

 #include <Carbon/Carbon.h> SetMenuItemIconHandle( GetApplicationDockTileMenu(), [dockMenu indexOfItem:dockMenuItem], kMenuIconResourceType, (Handle) CFSTR("icon.icns") ); 

Perhaps this is not so simple; some of them can only be 32-bit.

+1
source

Source: https://habr.com/ru/post/919958/


All Articles