Search code examples
objective-cmacosapplescriptfinder

Launch Finder window with specific files selected


I'm trying to programmatically launch an OS X Finder window from an Xcode project. I need the window to open to a specific folder and have specific files within that folder automatically selected.

This is similar to the "Show in Finder" functionality used in Xcode and related apps.

Does anyone know how to do this in either Objective-C, Swift, AppleScript, or Finder command-line parameters?


Solution

  • Objective-C version:

    NSArray *fileURLs = [NSArray arrayWithObjects:fileURL1, /* ... */ nil];
    [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];