Home > Utility > Writing Finder Comments from the Command Line

Writing Finder Comments from the Command Line

I had some old (we’re talking Apple IIGS era old) files that I wanted to keep around, and I wanted Spotlight to show them if there was a valid hit. Many of the file formats I cannot read anymore, but even a raw dump of the file could at least reveal the information I needed. What I could not find online was a way to write Spotlight-findable data from the command line. My idea was to run the strings command and embed that as a comment. I finally figured it out on my own, and it involves embedding AppleScript in a shell script via osascript.

Without further ado, here is the bash function that I keep in my .bash_profile file (along with this handy function for assigning Finder labels).

# Assign a finder comment to a file
  if [ $# -lt 2 ]; then
    echo "USAGE: findercomment file comment [words can continue]"
    echo "Sets the Finder comment for a file."
    echo "Example: findercomment stevejobs.jpg this one will be worth money some day"
    osascript - "$@" << EOF
    on run argv
    set text item delimiters of AppleScript to " "
    set theComment to (items 2 through end of argv) as string
    set theFile to POSIX file (item 1 of argv) as alias
      tell application "Finder"
        set comment of theFile to theComment
      end tell
    end run

With that function defined, I was able to recurse through a directory, run strings to extract words, and assign the words as a Finder comment to the files.

find . | while read F; do
  echo "$F" 
  COMMENTS=$(strings "$F")
  findercomment "$F" "$COMMENTS"

There's probably an extra step I could take to filter the output of strings against a dictionary, but I left it as is. Enjoy.

Categories: Utility Tags: , , , ,
  1. jhz
    December 15th, 2012 at 17:18 | #1

    Nice. Works perfectly (in my case, the goal was but the filename in a Finder Comment in order to change the name and keep track of the initial one.
    Thanks a lot.

  2. February 14th, 2014 at 08:08 | #2

    Aw, this was a very nice post. Taking the time and
    actual effort to generate a great article… but what can
    I say… I hesitate a whole lot and never seem to get anything

  3. March 20th, 2016 at 17:01 | #3

    Thanks on your marvelous posting! I really enjoyed reading it, you can be
    a great author. I will make certain to bookmark your blog
    and will come back someday. I want to encourage one
    to continue your great work, have a nice morning!

  1. No trackbacks yet.