Show icons in the dock contextual menus in OS X?
cocoa, macos, objective-c
Solution
The recent files list is actually part of the standard Dock icon menu. To use it in your app, you should build an `NSDocument`-based application. By using `NSDocument`, you will get the recent files menu/behaviour for free.
If your application cannot be based on `NSDocument`, you can instruct Cocoa to maintain a recent documents list based on URLs:
NSDocumentController *docController = [NSDocumentController sharedDocumentController];
[docController noteNewRecentDocumentURL:locationOfMyRecentFile1];
[docController noteNewRecentDocumentURL:locationOfMyRecentFile2];
[docController noteNewRecentDocumentURL:locationOfMyRecentFile3];
Note that currently, `-noteNewRecentDocumentURL:` only supports `file://` URLs (which you can create from a path with `+[NSURL fileURLWithPath:]`.) In the future, its behaviour will presumably change to allow URLs with other schemes.
Problem
My question is quite simple : To use a custom menu for the apps icon on the dock, `- (NSMenu*) applicationDockMenu: (id) sender;` of the `NSApplicationDelegate` has to return the menu that the dock will display. Using `setImage` on a `NSMenuItem`, you can normaly add icons to the menu. They show up on the normal menu, but not on in contextual menu of the app's dock icon. Then how did Apple manage QuickTime, XCode, Preview to show icons in the list of recent opened files accessible in their dock contextual menu ? Thx.