Launch OSX Finder window with specific files selected

I'm trying to programmatically launch an OSX 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.

Does anyone know how to do this in either objective c, applescript, or Finder command-line parameters?

Thanks!

Answers


Objective-C version:

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

$ open -R <path-to-reveal>

Another AppleScript flavor - the Finder's reveal command will both open a window to the containing folder and select the item(s). If there are multiple containing folders, multiple Finder windows will be opened.

tell application "Finder" 
   to reveal {someAlias, "path/to/POSIXfile" as POSIX file, etc}

I'm finding that activateFileViewerSelectingURLs is not working on Yosemite (at least when in separate space from Finder). It will cause a switch to the Finder's space but won't seem to select the URL. Using:

  • (BOOL)selectFile:(NSString *)fullPath inFileViewerRootedAtPath:(NSString *)rootFullPath

will switch spaces from full screen app and select path.


Swift version:

    let paths = ["/Users/peter/foo/bar.json"]
    let fileURLs = paths.map{ NSURL(fileURLWithPath: $0)}
    NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(fileURLs)

When opening a file at path:

NSString* path = @"/Users/user/Downloads/my file"
NSArray *fileURLs = [NSArray arrayWithObjects:[NSURL fileURLWithPath:path], nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];

Swift 3.2/4.0 Version: NSWorkspace.shared.activateFileViewerSelecting([outputFileURL])


Need Your Help

Android emulator error: "System UI has stopped"

android eclipse android-emulator adt

I have recently set up my Android development environment. Every thing is alright but when I run my emulator it takes to much time with an error dialog on the emulator screen says- "Unfortunately, ...

What is the difference between LR, SLR, and LALR parsers?

algorithm parsing compiler-construction grammar

What is the actual difference between LR, SLR, and LALR parsers? I know that SLR and LALR are types of LR parsers, but what is the actual difference as far as their parsing tables are concerned?