EpicDewd
EpicDewd

Reputation: 385

NSOpenPanel - Set file type?

Just looking on what I would use to only allow specific files to be selected (Images for now)

setFileTypesArray returns

NSOpenPanel may not respond to -setFileTypesArray:

and then the panel doesn't open up at all. Heres my code:

    NSArray  * fileTypes = [NSArray arrayWithObjects:@"png",@"tiff",@"baz",nil];

NSLog(@"Button Pressed");
[textField setStringValue:@"Test"];
int i; // Loop counter.

NSOpenPanel* openDlg = [NSOpenPanel openPanel];

[openDlg setCanChooseFiles:YES];
[openDlg setFileTypesArray:fileTypes];

Thanks.

Upvotes: 10

Views: 10265

Answers (6)

ingconti
ingconti

Reputation: 11646

my two cents for osx / swift 5 (You can specify title, and bring to "pictures" folder.

override func showChooseImageDialog(title: String){

    let openPanel = NSOpenPanel()
    openPanel.canChooseFiles = false
    openPanel.allowsMultipleSelection = false
    openPanel.canChooseDirectories = false
    openPanel.canCreateDirectories = false
    openPanel.title = title
    openPanel.allowedFileTypes = NSImage.imageTypes

    openPanel.directoryURL = URL(fileURLWithPath: picturesDir() )

    
    openPanel.beginSheetModal(for:self.view.window!) { (response) in
        if response == .OK {
            let selectedPath = openPanel.url!.path
            
            // do whatever you what with the file path
            
        }
        openPanel.close()
    }
    
}

Upvotes: 2

Peter Lapisu
Peter Lapisu

Reputation: 20975

You may wan to check out

[panel setAllowedFileTypes:[NSImage imageTypes]];

Or implement the delegate NSOpenSavePanelDelegate

and implement

- (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url {

    NSString * fileExtension = [url pathExtension];
    if (([fileExtension  isEqual: @""]) || ([fileExtension  isEqual: @"/"]) || (fileExtension == nil)) {
        return YES;
    }

    NSSet * allowed = [NSSet setWithArray:@[@"png", @"tiff", @"jpg", @"gif", @"jpeg"]];
    return [allowed containsObject:[fileExtension lowercaseString]];

}

Upvotes: 12

NSGodMode
NSGodMode

Reputation: 627

This did work for me :

 NSArray  * fileTypes = [NSArray arrayWithObjects:@"png",@"jpg",@"tiff",nil];

[openDlg setAllowedFileTypes:fileTypes];       

Upvotes: 2

smdvlpr
smdvlpr

Reputation: 1088

You're looking for a delegate method from NSSaveOpenPanel's delegate

-(BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
{
        NSString* ext = [filename pathExtension];
        if (ext == @"" || ext == @"/" || ext == nil || ext == NULL || [ext length] < 1) {
                return TRUE;
        }

        NSLog(@"Ext: '%@'", ext);

        NSEnumerator* tagEnumerator = [[NSArray arrayWithObjects:@"png", @"tiff", @"jpg", @"gif", @"jpeg", nil] objectEnumerator];
        NSString* allowedExt;
        while ((allowedExt = [tagEnumerator nextObject]))
        {
                if ([ext caseInsensitiveCompare:allowedExt] == NSOrderedSame)
                {
                        return TRUE;
                }
        }

        return FALSE;
}

Then, set your panel's delegate to "self", or wherever your define this method above.

Upvotes: 13

walkytalky
walkytalky

Reputation: 9543

The method you're looking for is setAllowedFileTypes -- see the docs for the parent class, NSSavePanel.

Upvotes: 8

Cesar A. Rivas
Cesar A. Rivas

Reputation: 1355

How about [openDlg setAllowedFileTypes:fileTypes];?

Upvotes: 29

Related Questions