Programmatically Adding the "Open Recent" Menu to the Context Menu

I have an application without a Cocoa document with a menu and status menu. I added the Open Recent menu to the status menu in Interface Builder. Filling the menu works very well:

[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: [NSURL fileURLWithPath:filename]] 

Now I would also like to add a second โ€œOpen Lastโ€ menu to the contextual context menu. How can I create a menu programmatically so that it is automatically filled with entries, as for the version in the status menu?

I tried to create a copy of one of them in the status menu, but it does not fill out. I assume that NSDocumentController does not know the menu (to be honest, I do not know how it knows about this in the status menu).

+4
source share
1 answer

For reference, the best documentation on the internal workings of the Open menu that I found is: http://lapcatsoftware.com/blog/2007/07/10/working-without-a-nib-part-5-open- recent-menu /

Unfortunately, this helps a little, because even if you create such a menu, it will be ignored by NSDocumentController . The menu must exist in the main menu before calling applicationDidFinishLaunching: otherwise it will not be selected - and, therefore, duplicated ones are also ignored.

What I have finished, and that seems to work so far, is this:

The first idea was to select the appropriate NSMenu from the main menu and attach it to other menus, hoping that link counting would do the job. There is no such luck, setSubmenu displays if the submenu is already in another NSMenuItem .

So, I change the submenu instead - when I need to show it in another menu, I delete it from the "Open last item" main menu and set it as a submenu in the new menu. Later I return it back. Of course, this is an ugly hack, but it does its job.

+2
source

All Articles