Terminal and Finder

Posted on December 15, 2012
Filed Under System | 8 Comments

Over at OSX Hints, as I noted on the sidebar earlier, there was a link to some functions at github integrating the Finder with the terminal. Most of these I was already doing as I suspect many of you were. I kind of like my versions better. I figured though it would be a nice catalyst to listing some great Finder integration to put in your .bash_profile. I’ll separate these out as I suspect you, like me, would like to pick and choose what you integrate. Some of these were already in my bash_profile this summer. But a few I’ve added since then and a few I’ve modified somewhat since then.

The one I didn’t include was an alias for open . since that’s so short as is. That opens the path you are currently at as a Finder window. I actually have an alias finder=’open .’ but I can’t recall the last time I used it. I wouldn’t bother.

selected: Gets the selected files in the Finder. This is slightly modified from the version this summer since I’d forgot to quote the returned paths then.

function selected()
    osascript -e 'tell application "finder"
                 set tf to selection
                 set tl to ""
                 repeat with f in tf
                    set tl to tl & "\"" & POSIX path of (f as alias) & "\" "
                 end repeat
               end tell'

cdf: Change to the directory of the frontmost window in the Finder. I use this one a lot.

function cdf() {
    cd "$(osascript -e 'try
        tell app "Finder" to (target of Finder window 1) as text
            POSIX path of result
        on error
            (system attribute "HOME") & "/Desktop"

lsfcom: Lists the finder comments for the specified files. You could pretty the output up a bit and this doesn’t handle multiline comments well. But it works well enough for me.

function finderComment () {
    for f in "$@" do
        mdls "$f" | grep kMDItemFinderComment

setfcom: Sets a Finder comment for a specified file. This is just an alias to the appropriate xattr command. To use just type something like setfcom “my comment” myfile and it should show up immediately. You can check with the lsfcom above. If you want to set tag metadata, which I’ve written a lot about here in the past, I’d suggest looking at the openmeta command line tool. I use it a lot in various scripts although not a ton at the command line.

Edit: The original worked inconsistently due to a weird bug where .DS_Store and xattr settings don’t sync properly. Weirdly it always worked for me before. Couldn’t tell you why. Here’s a better version using Applescript. The previous version I’ll list after the new version.

function setfcom() {
    osascript - "$@" << EOF
        on run argv
        set c to (items 2 through end of argv) as string
        set f to POSIX file (item 1 of argv) as alias
        tell application "Finder"
            set comment of f to c
          end tell
        end run
alias setfcom="xattr -w \"com.apple.metadata:kMDItemFinderComment\" "


  • http://lri.me/ Lauri Ranta

    The selected and cdf functions are affected by http://www.openradar.me/9406282. The selection and target properties refer to the second frontmost window after opening a new window.

    quoted form of POSIX path of (f as alias) would quote all paths properly.

    finderComment doesn’t work with filenames that have spaces because it uses for f in $* instead of for f in “$@”.

  • Clark

    Thanks Lauri. With regards to that first bug Daniel Jalkut submitted I’ve not personally encountered it. But it’s wise to know about. (Oh how I wish Apple supported its Applescript better)

    Good catch on the other. I modified it to show your suggestion.

  • http://lri.me/ Lauri Ranta

    Actually, the first issue only seems to affect selected. If I press ⌘N in Finder, select some files, switch to Terminal, and run selected, it doesn’t return anything.

    I can’t get comments added with xattr to show up in Finder’s information windows either. The Spotlight comments are stored in both .DS_Store files and extended attributes, but Finder only sees the .DS_Store files.

  • Clark

    What version of OSX are you running Lauri? I just tried to replicate the Finder selection you mentioned and it works fine for me.

    The xattr thing seems weird. Sometimes it works and sometimes it doesn’t. But I just replicated it not working. Something is sometimes syncing .DS_Store with extended attributes but I can’t tell what. I updated it with an Applescript version modified from here. (Updated original)

  • http://lri.me/ Lauri Ranta

    I have an almost fresh installation of 10.8.2, and I can still reproduce it every time. (I’ve tried to find better workarounds for that bug for hours.)

    Another bug with the new version of setfcom is that osascript shows a warning like “CFURLGetFSRef was passed this URL which has no scheme” when a relative path is converted to an alias on 10.8. You can just convert the paths to absolute paths though.

    setfcom() {
    osascript – "$PWD" "$@" <<'END'
    on run argv
    repeat with f in items 3 thru -1 of argv
    if f does not start with "/" then set f to item 1 of argv & "/" & f
    posix file f as alias
    tell app "Finder" to set comment of result to item 2 of argv

    setfcom comment test ../test ~/test .

  • Clark

    Weird. I finally had it happen but it’s really inconsistent. I wonder why that is? In any case the fact the bug is still there unfortunately demonstrates the respect Apple holds Applescript at the moment.

    Thanks for the update fixing relative paths.

  • Pingback: Front Finder Path : Clark's Tech Blog

  • Pingback: Copy Finder Selection Paths : Clark's Tech Blog