How do I show a file chooser dialog on Mac OS X? The language is Objective C.
What you search is 'NSOpenPanel', here a example how to use:
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:YES]; // yes if more than one dir is allowed
NSInteger clicked = [panel runModal];
if (clicked == NSFileHandlingPanelOKButton) {
for (NSURL *url in [panel URLs]) {
// do something with the url here.
}
}
Those who are looking for Swift version
let panel = NSOpenPanel()
panel.canChooseDirectories = false
panel.canChooseFiles = true
panel.allowsMultipleSelection = false
panel.allowedFileTypes = ["txt"]
let clicked = panel.runModal()
if clicked == NSApplication.ModalResponse.OK {
print("URLS => \(panel.urls)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With