Question

I've got some action in my application on OS X where I have to select file from finder. I want to display window like: "Open file". I know that this let me open url with path:

[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES]];

But how to show window with "Select" button. This window should let me to get info about selected file.

How can I do this correctly?

Thank you for help.

Was it helpful?

Solution 3

@Perception and @omz gives me good answer. Answer is NSOpenPanel.

OTHER TIPS

The code for previous answers :

NSOpenPanel *op = [NSOpenPanel openPanel];
op.canChooseFiles = YES;
op.canChooseDirectories = YES;
[op runModal];
self.txtFilePath.stringValue = [op.URLs firstObject];

in op.URLs you can find paths for all files you just selected.

Building on EderYif's answer, the following doesn't produce a compiler warning and also removes the 'file://' part of the returned filename.

NSOpenPanel *op = [NSOpenPanel openPanel];
[op setCanChooseFiles:true];
[op setCanChooseDirectories:true];
[op runModal];

NSString* file = [[op.URLs firstObject] absoluteString];
NSString* fixedFile = [file stringByReplacingOccurrencesOfString:@"file://"
                                                      withString:@""];
[[self textFilePath] setStringValue:fixedFile];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top