Johnny Mast
Johnny Mast

Reputation: 703

Display file chooser dialog

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

Upvotes: 16

Views: 12075

Answers (2)

Anand
Anand

Reputation: 5332

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)")
}

Upvotes: 5

evotopid
evotopid

Reputation: 5429

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.
    }
}

Upvotes: 33

Related Questions