18

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.

10 Answers 10

9

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

4

The osascript methods seemed broken for me in Mavericks AppleScript (and I haven't needed to try them since), but this works:

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

(This marks the file as Red, you'll have to reverse-engineer other colours).

Under Mavericks this seems to merge the file label with the previous one (as they're now "tags").

In case it isn't obvious, this is Q&D and could break in the future, but it works (and is muuuch faster than AppleScript) in at least:

  • 10.9, HFS+
  • 11.6, APFS
3
  • Oh my, that's quite the command. Dec 16, 2016 at 21:26
  • Sadly the -x option has been deprecated, so this no longer works (I'm using Big Sur) Nov 9, 2021 at 13:15
  • 'deprecated' doesn't mean 'removed', just tried it and it still works on my Big Sur machine. And the -x flag is still there in the man page (which is almost 11 years old!) so I'm not actually sure it is deprecated. This remains very muvh a Q&D method that could break with any update
    – Piersg
    Nov 12, 2021 at 22:26
3

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

2
  • 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, 2011 at 0:10
  • You escape it with backslashes: File\ with\ Spaces.txt
    – msanford
    Feb 28, 2011 at 2:05
3

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
1
  • 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, 2012 at 12:36
1

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.

1
  • Unfortunately, osxutils is PPC only.
    – user194149
    Nov 28, 2014 at 1:28
1

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.

1

I love these scripts, however, they weren't working for my files that used spaces in their names until I changed the IFS setting for bash within the script, also I changed the file input to accept a text file with a list of filenames:

#!/bin/bash
# Set Finder label color of files in a list
# set the Internal Field Separator to \n (newline)
IFS=$'\n'
  if [ $# -lt 2 ]; then                                                       
    echo "USAGE: LabelFilelist color Path/to/filelist ..."
    echo "Sets the Finder label (color) for files"
    echo "Possible colors: None Orange Red Yellow Blue Purple Green Gray"
  else

 labelargs=$@
  color=$1
  file=`cat < $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
1

I can't yet add comments to @Piersg's answer, but here is a list of the available tags based on his command, confirmed in macOS 11.6.2:

xattr -wx com.apple.FinderInfo "0000000000000000000100000000000000000000000000000000000000000000" folder

Modify the "1" above to any of the following:

  • 0: no label
  • 2: gray
  • 4: green
  • 6: purple
  • 8: blue
  • A: yellow
  • C: red
  • E: orange
0

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.

2
  • how would you call that from a command-line?
    – Svish
    Jul 28, 2010 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, 2010 at 19:53
0

You can use the xattr command to add a tag:

# Set to the image.jpg, the tag "Red" with the red color
xattr -w com.apple.metadata:_kMDItemUserTags '("Red\n6")' image.jpg
# Set to the image.jpg, the tag "Green" with the green color, and the tag "Important"
xattr -w com.apple.metadata:_kMDItemUserTags '("Green\n2","Important")' image.jpg

The default color tags are color_name + \n (new line / line feed) + color_index. Where colors index are:

  1. none
  2. grey
  3. green
  4. purple
  5. blue
  6. yellow
  7. red
  8. orange

To read the labels of a file:

mdls -plist - -name _kMDItemUserTags image.jpg | plutil -convert json -o - -
# > {"_kMDItemUserTags":["Green\n2","Important"]}

About xattr and _kMDItemUserTags: xattr: com.apple.metadata:_kMDItemUserTags, Finder tags – The Eclectic Light Company

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .