Mac OS X: How to change the color label of files from the Terminal

colorslabel;macterminal

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.

Best Answer

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
}
>

Related Question