开发者

How to open a folder?

After save a file I want to open the f开发者_如何学运维older of saved file. How do I do that? Thank you very much!


If I understand your question, you want to open the folder into which something was saved in the Finder?

This should do the trick -- it assumes that you have a reference to the savePanel.

NSURL *fileURL = [savePanel URL];
NSURL *folderURL = [fileURL URLByDeletingLastPathComponent];
[[NSWorkspace sharedWorkspace] openURL: folderURL]; 

If you are starting with an NSString containing the path, then start with:

NSURL *fileURL = [NSURL fileURLWithPath: stringContainingPath];


Even better would be to not just open the folder, but have the saved file selected. NSWorkspace can do that for you:

[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:
   @[ URLToSavedFile ]];

The argument is an array of URLs, so if you have only one file you want to reveal, you simply pass an array of one object.

If, for some reason, you're targeting a version of Mac OS X older than 10.6, you'd use the older path-based method instead:

[[NSWorkspace sharedWorkspace] selectFile:pathToSavedFile 
                 inFileViewerRootedAtPath:@""];

(You want to pass an empty string for the second argument so that the Finder will reuse an existing Finder window for the folder, if there is one.)


I know this post is fairly old, but with 10.9 what you want to do is

NSString* folder = @"/path/to/folder"  
[[NSWorkspace sharedWorkspace]openFile:folder withApplication:@"Finder"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜