Search code examples
objective-cmacoscocoafindernsworkspace

Open file in Finder window OSX


My question is about opening a Finder window to show the file.

To open a single file, able to do by this :

[[NSWorkspace sharedWorkspace] selectFile:file.path 
                 inFileViewerRootedAtPath:file.path];

Now I need to have a single Finder if user tries to open the same file.path.

To achieve this, I add file.path in an array.

But I am stuck when user closes the Finder window and again tries to open the file (as the active window has THE array with the path). Then Finder doesn't show :(

Any help how to proceed on this.


  1. Is there a way to check the path for which the Finder window is open?

  2. Is there a way to get a callback for the finder, so that I can remove the file.path from the array?


Solution

  • It is done by simply passing nil.

    [[NSWorkspace sharedWorkspace] selectFile:file.path inFileViewerRootedAtPath:nil];
    

    Now same file / folder is selected even on multiple clicks.