jump to navigation

Writing Finder Comments from the Command Line August 7, 2011

Posted by Robert Harder in Utility.
Tags: , , , ,
trackback

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).

[bash]# Assign a finder comment to a file
findercomment(){
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”
else
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
return
end run
EOF
fi
}[/bash]

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.

[bash]find . | while read F; do
echo “$F”
COMMENTS=$(strings “$F”)
findercomment “$F” “$COMMENTS”
done[/bash]

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.

Comments»

1. jhz - December 15, 2012

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. staples coupons 2012 - February 14, 2014

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
done.

3. living with diabetes - March 20, 2016

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!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: