A couple things I noticed.. change setCanChooseDirectories
to NO. When enabled this indicates that folders are valid input. This is most likely not the functionality you want. You might also want to change your allowed file types to [NSArray arrayWithObject:@"pdf", @"PDF", nil]
for case sensitive systems. runModalForTypes
should be the array of file types. Change your code to look like this:
// WORKING :)
NSOpenPanel *panel;
NSArray* fileTypes = [NSArray arrayWithObjects:@"pdf", @"PDF", nil];
panel = [NSOpenPanel openPanel];
[panel setFloatingPanel:YES];
[panel setCanChooseDirectories:NO];
[panel setCanChooseFiles:YES];
[panel setAllowsMultipleSelection:YES];
[panel setAllowedFileTypes:fileTypes];
int i = [panel runModal];
if(i == NSOKButton){
return [panel URLs];
}
Swift 4.2:
let fileTypes = ["jpg", "png", "jpeg"]
let panel = NSOpenPanel()
panel.canChooseFiles = true
panel.canChooseDirectories = false
panel.allowsMultipleSelection = false
panel.allowedFileTypes = fileTypes
panel.beginSheetModal(for: window) { (result) in
if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
// Do something with the result.
let selectedFolder = panel.urls[0]
print(selectedFolder)
}
}