Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

7 Answers 7

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

share|improve this answer
1  
awesome, thanks! –  Ortwin Gentz Oct 17 '11 at 14:44
    
This is an amazing script. Very useful. Thank you! –  bafromca Aug 1 '13 at 16:25

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

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

Here's my version, based on the two from @Lauri and @Robert. You specifiy the color using the name of the color, not the number. The color names are consistent with the output of hfsdata -L, so you use "None" to assign no color to the file. Save this in a file called "setlabel" and do chmod 755 setlabel.

#!/bin/bash
# Set Finder label color
  if [ $# -lt 2 ]; then                                                       
    echo "USAGE: setlabel color file1 [file2] ..."
    echo "Sets the Finder label (color) for files"
    echo "Possible colors: None Orange Red Yellow Blue Purple Green Gray"
  else
  labelargs=$@
  color=$1
  file=$2
  colorarray=( None Orange Red Yellow Blue Purple Green Gray )
  colorvalue=8
  for i in {0..7}
     do
      if [ "${color}" == ${colorarray[${i}]} ]
      then
         colorvalue=${i}
      fi
     done
  if [ "${colorvalue}" == "8" ]
      then
         echo Color ${color} is not recognized.
     echo "Possible colors: None Orange Red Yellow Blue Purple Green Gray"
     else
    osascript - ${colorvalue} ${file} << EOF >/dev/null 2>&1
    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
  fi
share|improve this answer
    
You may want to edit your answer to reference the other answers by their authors' @names. "The two above" is potentially useless, as a user can order these posts differently if they'd like. –  JoshP Sep 18 '12 at 12:36

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.

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

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.

share|improve this answer
    
Unfortunately, osxutils is PPC only. –  megadroid Nov 28 at 1:28

This would use the same order for the colors as Finder.

#!/bin/bash

if [[ $# -le 1 || ! "$1" =~ ^[0-7]$ ]]; then
  echo "usage: label 01234567 FILE..." 1>&2
  exit 1
fi

colors=( 0 2 1 3 6 4 5 7 )
n=${colors[$1]}
shift

osascript - "$@" <<END > /dev/null 2>&1
on run arguments
tell app "Finder"
repeat with f in arguments
set f to (posix file (contents of f) as alias)
set label index of f to $n
end
end
end
END

stderr is redirected because converting a relative path to an alias results in a warning like CFURLGetFSRef was passed this URL which has no scheme on 10.8. stdout is redirected because osascript prints the value of the last expression.

share|improve this answer

The osascript methods seem broken in Mavericks AppleScript, but this seems to work:

xattr -wx com.apple.FinderInfo "0000000000000000000C00000000000000000000000000000000000000000000" /path/to/your/file

Under Mavericks this seems to merge the file label with the previous one (as they're now "tags") and by the same token I'd expect the above to break at some point if Apple stop usinf extended attributes in this way. But it has the advantage of working for me now and being a lot quicker than AS.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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