Matt Rajca

Jul 09 2011

AppKit Quick Tip: Populating the ‘Open Recent’ Menu in Non-Document-based Applications

While writing Nibble, my native Apple I emulator for Mac OS X and iOS, I realized an Open Recent menu would be beneficial to users who want to quickly open recently-loaded memory dumps. In standard Document-based applications, AppKit automatically registers newly-opened files as recents. Unfortunately, it doesn’t make much sense to make an emulator a Document-based application.

Luckily, even Non-Document-based applications contain an Open Recent menu, ready to be populated. Moreover, NSDocumentController makes it extremely easy to register recently-accessed files through itsĀ noteNewRecentDocumentURL: method. AppKit will magically append the file at the passed URL to the Open Recent menu; it will even enable the Clear Menu item when necessary! When the user selects a recent file, AppKit will invoke the application:openFile: method on NSApplication’s delegate object.

The complete code listing can be found below:

NSDocumentController *dc = [NSDocumentController sharedDocumentController];
[dc noteNewRecentDocumentURL:[NSURL fileURLWithPath:somePath]];


- (BOOL)application:(NSApplication *)sender
           openFile:(NSString *)filename {

    // load the file

    return YES;
}

3 notes

  1. mattrajca posted this
Page 1 of 1