Is there a way I can set the color label of a file to some color when in the Terminal?

I know that the following command lists some info about what the color currently is, but I can't figure out how to do something about it. Like change it.

mdls -name kMDItemFSLabel somefile.ext

The reason I would like to know is that I want to recursively mark all files in a folder of a certain type with a certain color label (in my case gray).

I know how to do the finding:

find . -name "*.ext"

And I know how I can run the command afterwards for each file using -exec, but I need to know how to do the actual labeling...

I would like a solution that only involves commands built-in to Mac OS X. So preferably no 3rd party stuff, unless there is no other way.

link|improve this question

68% accept rate
feedback

4 Answers

osascript -e "tell app \"Finder\" to set label index of POSIX file (\"/junk.txt\") to 1"

link|improve this answer
osascript -e "tell app \"Finder\" to set label index of POSIX file (\"/junk.txt\") to 1 What if junk.txt is really my full/path/with spaces.txt and stored in a variable called $fileName I've tried countless syntaxes and single-quotes, double-quotes... and none of them work. – user69451 Feb 28 '11 at 0:10
You escape it with backslashes: File\ with\ Spaces.txt – msanford Feb 28 '11 at 2:05
feedback

Based on the responses here and in referenced posts, I made the following function and added it to my ~/.bash_profile file:

# Set Finder label color
label(){
  if [ $# -lt 2 ]; then
    echo "USAGE: label [0-7] file1 [file2] ..."
    echo "Sets the Finder label (color) for files"
    echo "Default colors:"
    echo " 0  No color"
    echo " 1  Orange"
    echo " 2  Red"
    echo " 3  Yellow"
    echo " 4  Blue"
    echo " 5  Purple"
    echo " 6  Green"
    echo " 7  Gray"
  else
    osascript - "$@" << EOF
    on run argv
        set labelIndex to (item 1 of argv as number)
        repeat with i from 2 to (count of argv)
          tell application "Finder"
              set theFile to POSIX file (item i of argv) as alias
              set label index of theFile to labelIndex
          end tell
        end repeat
    end run
EOF
  fi
}
>

link|improve this answer
awesome, thanks! – Ortwin Gentz Oct 17 '11 at 14:44
feedback

Here are two articles describing how to do that using applescript, which can in turn, be invoked from the command line.

How to set color label via Terminal or applescript
and
tagging files with colors in os-x finder from shell scripts.

link|improve this answer
how would you call that from a command-line? – Svish Jul 28 '10 at 18:32
In AppleScript Editor, you can compile and save a script as an application. You can run that by specifying its path. You can run on line of AppleScript by preceding it with "osascript" and quoting the Applescript command. The quoting can get complex, sometimes... – JRobert Jul 28 '10 at 19:53
feedback

To view them in the Finder (I know, not what you asked) you can use xattr -l, or xattr -p com.apple.FinderInfo, you get a flag among the zeroes (1E), of which the lower bits are the colour.. With third party stuff: hfsdebug (use with sudo) to get a lot of info, among which a readable colour label.

To change them with third part stuff: osxutils has a setlabel command.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.