سؤال

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];

}
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

From a sanity-check point of view: If the user running the process which makes the [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[ fileURL ]] call doesn't have read/view permissions for fileURL then the method will appear to "not work".

To determine if this is your issue (As of OS X 10.10.3), check your console for logs like the following:

6/18/15 10:34:46.195 AM NameOfApp[42753]: AppleEvents failed to encode extension for /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/xxxxxxxxxxx/yyyy.zzz, err=Permission denied/13
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top