Documentation Archive Developer
Search

ADC Home > Reference Library > Technical Q&As > Cocoa > User Experience >

How to remove the "Open Recent" menu item in a Document-based Cocoa application


Q: How do you remove the "Open Recent" menu item in a Document-based Cocoa application?

A: Some developers may want to remove the "Open Recent" menu item permanently from the File menu in their document-based Cocoa applications. Simply removing the menu item from the nib in Interface Builder does not suffice, however, because NSDocumentController checks for that menu item's presence and reinserts it at launch time. Instead, to get rid of it for good, you need to remove it at runtime after NSDocumentController has added it. The best place to remove the menu item is in the application delegate method -applicationDidFinishLaunching: See code in Listing 1 for how to do this. Note that Apple's Aqua Interface Guidelines recommend the inclusion of the "Open Recent" menu item.


[Sep 15, 2003]



Listing 1. Removing the 'Open Recent' menu item


    // fileMenu is an outlet you create to the File menu in your application
    int openDocumentMenuItemIndex = [fileMenu indexOfItemWithTarget:nil
andAction:@selector(openDocument:)];

    if (openDocumentMenuItemIndex>=0 &&
        [[fileMenu itemAtIndex:openDocumentMenuItemIndex+1] hasSubmenu])
    {
            // We'll presume it's the Open Recent menu item, because this is
            // the heuristic that NSDocumentController uses to add it to the
            // File menu
            [fileMenu removeItemAtIndex:openDocumentMenuItemIndex+1];
    }