NSOpenPanel - Select only folders

Discussion in 'Mac Programming' started by MorphingDragon, Jun 18, 2011.

  1. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Messages:
    5,161
    Location:
    The World Inbetween
    #1
    I'm having issues getting an NSOpenPanel to only select folders. File selection is false but for some reason I can still select files.

    Code:
    //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];
    
     
  2. jiminaus, Jun 18, 2011
    Last edited: Jun 18, 2011

    macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Messages:
    1,448
    Location:
    Sydney
    #2
    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.
     
  3. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Messages:
    5,161
    Location:
    The World Inbetween
    #3
    I cleaned my project build and now it works fine. :confused:
     

Share This Page