Select files in Finder from Terminal

May 24, '06 07:30:05AM

Contributed by: Lutin

I wanted to be able, from the Terminal, to select files in the Finder. That is, to use a command like this...

showinfinder /Developer/Tools/GetFileInfo /Developer/Tools/SetFile
...and have Terminal open the Finder, with the specified file(s) selected.

The command open wasn't useful in this case, because I wanted the files to be selected. And sometimes I have the complete path file in the clipboard, and it annoys me to have to strip the name off, and then in the Finder, look for this file.

My solution is an AppleScript called via a function in the shell. This way, you can also use completions from the shell to specify one or several files. If you call it normally, you will have to give the arguments with an absolute path. But, if the first argument is Current_path:path, then the script will look in the folder path to find files.

Next I added this function in my alias file (I use the zsh shell). It takes care of providing the current directory:

function showinfinder() {
    thepath=`pwd`;
    args="";
    while [ $# -ne 0 ]; do
        args=`echo "$args $1"`;
        shift;
    done
    echo "/path/to/the/script/FinderSelect.scpt" \
    "Current_path:$thepath" "$args" | xargs osascript;
}
Make sure you replace /path/to/the/script/FinderSelect.scpt with the actual (full) path to your saved AppleScript. And here's an example of how it's used:
showfinder -a toto /tmp/toto2
This will add /tmp/toto2 and toto from the current directory to the current selection, and show them in the Finder. Like the script from this hint, I have the same bug and have no idea how to fix it. Any ideas?

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060519105305580