PDA

View Full Version : NSOpenPanel - Select only folders




MorphingDragon
Jun 18, 2011, 08:29 AM
I'm having issues getting an NSOpenPanel to only select folders. File selection is false but for some reason I can still select files.


//Create open panel dialog
NSOpenPanel *open = [NSOpenPanel openPanel];

//Disable file selection
[open setCanChooseFiles: false];

//Enable folder selection
[open setCanChooseDirectories: true];

//Enable alias resolving
[open setResolvesAliases: true];

//Disable multiple selection
[open setAllowsMultipleSelection: false];

//Display open panel
[open runModal];

//Get source folder name
NSURL* source = [[[open URLs] objectAtIndex: 0] retain];



jiminaus
Jun 18, 2011, 09:07 AM
Works for me under Mac OS X 10.6.7. Files are grayed-out. The open button is always enabled. If nothing is selected, then the "current folder" is chosen.

BTW NSOpenPanel inherits URL from NSSavePanel. So instead of [[open URLs] objectAtIndex: 0], you can simply use [open URL].

And don't forget to check the return value of runModal. It will be NSFileHandlingPanelOKButton if the user clicks Open, otherwise it will be NSFileHandlingPanelCancelButton.

MorphingDragon
Jun 18, 2011, 09:05 PM
Works for me under Mac OS X 10.6.7. Files are grayed-out. The open button is always enabled. If nothing is selected, then the "current folder" is chosen.

BTW NSOpenPanel inherits URL from NSSavePanel. So instead of [[open URLs] objectAtIndex: 0], you can simply use [open URL].

And don't forget to check the return value of runModal. It will be NSFileHandlingPanelOKButton if the user clicks Open, otherwise it will be NSFileHandlingPanelCancelButton.

I cleaned my project build and now it works fine. :confused: