Reputation: 4235
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.
Upvotes: 1
Views: 2952
Reputation: 167
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];
Upvotes: 2
Reputation: 1619
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.
Upvotes: 6
Reputation: 4235
@Perception and @omz gives me good answer. Answer is NSOpenPanel
.
Upvotes: 0