XCode: Show folder/file in Finder by button click

Sometimes you have to implement a method that launch the Finder programmatically, e.g. to show a file or open a folder in Finder.
Therefore you can use NSWorkspace.

To open a folder in Finder by button click use an IBAction method similar to this.
We are using here Objective C.

//is called, when a specific button is pressed
- (IBAction)openFinderPressed:(id)sender {
    if (!(_folderURL == nil)) {
        [[NSWorkspace sharedWorkspace] openURL:_folderURL];

If you want select directly a file in Finder you can call

[[NSWorkspace sharedWorkspace] selectFile:_filePath inFileViewerRootedAtPath:nil];

The [NSWorkspace sharedWorkspace] method is available in OS X v10.0 and later.

The code looks so easy. I needed that method to run in my project Icon Scaler. But I know some month later I will have forgotten that fancy code snippet. But now I know, where I have to look ;-)


comments powered by Disqus

Copyright Dunkel & Iwer GbR | Datenschutzerklärung | Impressum