Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display file chooser dialog

How do I show a file chooser dialog on Mac OS X? The language is Objective C.

like image 772
Johnny Mast Avatar asked Feb 26 '11 11:02

Johnny Mast


2 Answers

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.
    }
}
like image 142
evotopid Avatar answered Oct 28 '22 21:10

evotopid


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)")
}
like image 33
Anand Avatar answered Oct 28 '22 19:10

Anand