Search code examples
cocoansworkspace

Show in Finder not working in 10.8


This works in 10.7 but not in 10.8. How do I get this to work in Mountain Lion?

-(IBAction)showTargetInFinder:(id)sender
{

     NSString *linkPath = [targetField stringValue];
     NSWorkspace *ws = [NSWorkspace sharedWorkspace];
     [ws selectFile:linkPath inFileViewerRootedAtPath:nil];

}

Solution

  • Use this code instead:

    NSURL *fileURL = your file URL;
    NSArray *fileURLs = [NSArray arrayWithObjects:fileURL, nil];
    [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
    

    This works on 10.6+. If you want to show and select more than one file on Finder just add more NSURLs to the array and you're good to go.