How do I show a file chooser dialog on Mac OS X? The language is Objective C.
Asked
Active
Viewed 1.2k times
16
-
What programming language? Cocoa, carbon, etc.? What have you already tried? – Delan Azabani Feb 26 '11 at 11:44
-
@Delan Azabani, If he's asking this kind of question, he's probably talking Cocoa. Maybe Java. But you're right, this is far too ambiguous right now. – Aurum Aquila Feb 26 '11 at 11:46
-
Sorry the language is objective C i have not tried anything yet because right now im developing an iPhoto library API as i am awaiting an reply on this question. – Johnny Mast Feb 26 '11 at 13:21
2 Answers
33
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.
}
}

Brad The App Guy
- 16,255
- 2
- 41
- 60

evotopid
- 5,288
- 2
- 26
- 41
-
Thanks a lot that did it !. Only i had to comment out the click== part as NSFileHandlingPanelOkButton as not defined. – Johnny Mast Feb 26 '11 at 13:41
-
It could be because of the fact that i am developing Mac OS Lion that NSFileHandlingPanelOkButton was not found. It could have been deprecated ?. – Johnny Mast Feb 26 '11 at 13:46
-
but, i think it's a small misspelling, try: NSFileHandlingPanelOKButton, With OK and not Ok ;-) – evotopid Feb 26 '11 at 13:56
-
1It's been a while since I last programmed something using the Cocoa framework. According to [the documentation](https://developer.apple.com/Library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSOpenPanel_Class/index.html) you need the `@import AppKit;`... – evotopid Nov 15 '14 at 15:25
5
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)")
}

Anand
- 5,323
- 5
- 44
- 58