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.

john paterson
Mar 9, 2012 -
this worked great for me apart from the line:
[openDlg setAllowedFileTypes:fileTypesArray];
which threw up this error
Thread 1:EXC_BAD_ACCESS(code=1,address=0×10)
Any idea what this is?
Otherwise great piece of code that have saved me a lot of time. thank you.
mcteapot
Mar 9, 2012 -
Are you closing out the NSArray you are passing in with a nil?
mcteapot
Mar 9, 2012 -
Ok, fixed it! I forgot to put a @ in front of the string literals , such a n00b mistake. Thanks for pointing it out.
whateva
Jul 18, 2012 -
Worked great! You saved me alot of time, thanks.
One thing: The NSURL didn’t worked for me ([NSURL URLWithString:])
mcteapot
Jul 18, 2012 -
Not sure why you are trying to manually set an NSURL? The URL returned by the NSOpenPanel should be an array of URLs from the selected files.
marty
Sep 15, 2012 -
You have one ] to much in your NSLog line.
Thanks a lot for the code!
mcteapot
Sep 15, 2012 -
thanks for looking out
ruedi
Sep 28, 2012 -
Hi
Since upgrading to 10.8 and Xcode4.5, my similar code in existing projects doesn’t work any more.
The panel opens, but then the app crashes with a message in the Debug Navigator as:
quicklook.pluginload(serial)
1 Thread
Thread 5
14 _pthread_wqthread
In the console, a couple of warnings appear:
WindowServer: CGXDeferSurfaces: Invalid source window 19938
and another warning:
28.September.12 12:10:40.001 Xcode[78227]: [MT] DVTAssertions: Warning in /SourceCache/IDEKit/IDEKit-1854/Framework/Classes/Editor/IDEEditorContext.m:617
Details: Lost history for x-xcode-disassembly://stack_frame?processID=31774&threadID=12&frameID=0
Object:
Method: -_greatestDocumentAncestorWasForgotten
Thread: {name = (null), num = 1}
Please file a bug at http://bugreport.apple.com with this warning message and any useful information you can provide.
Any ideas?
Thanks
Ruedi
Marcus
Nov 2, 2012 -
I just had the same crash. After I deleted the exception breakpoint I had set in Xcode, it worked fine. HTH
Marcus
Arthur
Mar 8, 2013 -
Thanks you so much, I never would have guessed.
NSOpenPanel: Displaying a File Open Dialog in OS X 10.7 | Wanderer
Nov 7, 2012 -
[...] NSOpenPanel: Displaying a File Open Dialog in OS X 10.7 This entry was posted in Uncategorized by admin. Bookmark the permalink. [...]
maxsumae
Nov 29, 2012 -
Thanks for the code, but I could not get it to work… until… I updated my “sandbox” settings to allow access… Would be nice if XCODE would warn you when you are asking for a sandboxed function. Sigh… But your code works fine once I got my head out of the sand…
max
Jan 1, 2013 -
thanks