45

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?

pkamb
  • 33,281
  • 23
  • 160
  • 191
simon.d
  • 2,471
  • 3
  • 33
  • 51
  • [How do I use AppleScript to reveal a file in Finder from its POSIX path? - Apple - Stack Exchange](http://apple.stackexchange.com/questions/26535/how-do-i-use-applescript-to-reveal-a-file-in-finder-from-its-posix-path/26579#26579) – Lri Oct 04 '11 at 19:50
  • @user979133 I would be very surprised if Finder had any command-line parameters other than those every Cocoa app has (the only ones I know of are adding file paths to open them). – Cajunluke Oct 04 '11 at 23:46
  • See [for Swift code to Launch OS X Finder](http://stackoverflow.com/questions/30738052/show-folders-contents-in-finder-using-swift/34578013#34578013) – Hugo Pereira Jan 03 '16 at 15:33
  • To Launch OS X Finder and select items with Swift go to this answer [Launch OS X Finder and select items with Swift](http://stackoverflow.com/a/34578013/4306496) – Hugo Pereira Jan 03 '16 at 15:36

8 Answers8

91

Objective-C version:

NSArray *fileURLs = [NSArray arrayWithObjects:fileURL1, /* ... */ nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
fireshadow52
  • 6,298
  • 2
  • 30
  • 46
Wevah
  • 28,182
  • 7
  • 83
  • 72
  • I'm curious why this was downvoted; this does exactly what the asker asked for. – Wevah Oct 05 '11 at 18:58
  • 6
    Works quite well. The only comment I have is that the array is an array of NSUrl's. It almost seems obvious given that the name of the array is "fileURL's", but I for some reason put a NSString in with a path, and that crashed rather spectacularly. Instead constructing an NSUrl from the path string (NSURL* url = [NSURL fileURLWithPath:fileToShow isDirectory:dir];) and then including that in the array worked perfectly. – John Bowers Dec 03 '12 at 20:18
20
$ open -R <path-to-reveal>
Matt McClure
  • 16,009
  • 2
  • 28
  • 30
  • 4
    This works for single files, but not for multiple: `open -R foo.jpg bar.jpg` won't select both `foo.jpg` and `bar.jpg`. – mjs Apr 17 '15 at 10:50
10

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}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
4

Swift version:

let paths = ["/Users/peter/foo/bar.json"]
let fileURLs = paths.map{ NSURL(fileURLWithPath: $0)}
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(fileURLs)
pkamb
  • 33,281
  • 23
  • 160
  • 191
Klaas
  • 22,394
  • 11
  • 96
  • 107
1

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.

Wevah
  • 28,182
  • 7
  • 83
  • 72
1

Reveal multiple files in Finder

As open -R <path-to-reveal> only works for single file. We can use Apple Script instead.

From user866649's answer, we can port it to a shell script as the following:

osascript -e 'tell application "Finder" to reveal {"path/to/file1" as POSIX file, "path/to/file2" as POSIX file} activate'

Just created a utility script:

finder.sh

#!/usr/bin/env bash

join() {
  local d=$1 s=$2
  shift 2 && printf %s "$s${@/#/$d}"
}

lst=()
for f in "$@"; do
  lst+=("\"$f\" as POSIX file")
done
files=$(join , "${lst[@]}")

osascript -e "tell application \"Finder\" to reveal {$files} activate"

Then try it:

chmod +x finder.sh
./finder.sh ~/Downloads ~/Desktop

It should open Finder and selects both Downloads and Desktop folder.

ninhjs.dev
  • 7,203
  • 1
  • 49
  • 35
0

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];
maxisme
  • 3,974
  • 9
  • 47
  • 97
0

Swift 3.2/4.0 Version:

NSWorkspace.shared.activateFileViewerSelecting([outputFileURL])
pkamb
  • 33,281
  • 23
  • 160
  • 191
webcpu
  • 3,174
  • 2
  • 24
  • 17