I’ve been coding a lot in Cocoa lately. When looking up how to create a file open dialog with NSOpenPanel, I found out that most of the source code on the web was out of date. With the release of OS X 10.7 a lot of the methods for NSOpenPanel have been deprecated. So, here is a code sample on how to create a file open dialog box and save your self from a few warnings in Xcode 4.

//NSOpenPanel: Displaying a File Open Dialog in OS X 10.7

// Any ole method
- (void)someMethod {

  // Loop counter.
  int i;

  // Create a File Open Dialog class.
  NSOpenPanel* openDlg = [NSOpenPanel openPanel];

  // Set array of file types 
  NSArray *fileTypesArray;
  fileTypesArray = [NSArray arrayWithObjects:@"jpg", @"gif", @"png", nil];

  // Enable options in the dialog.
  [openDlg setCanChooseFiles:YES];    
  [openDlg setAllowedFileTypes:fileTypesArray];
  [openDlg setAllowsMultipleSelection:TRUE];

  // Display the dialog box.  If the OK pressed,
  // process the files.
  if ( [openDlg runModal] == NSOKButton ) {

    // Gets list of all files selected
    NSArray *files = [openDlg URLs];

    // Loop through the files and process them.
    for( i = 0; i < [files count]; i++ ) {

      // Do something with the filename.
      NSLog(@"File path: %@", [[files objectAtIndex:i] path]);




Note: If you plan to store the paths for later use, it is advisable to keep the objects as a NSURL and not convert them to a NSString. In turn giving you more flexibility down the line.