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

17

12

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.

Svish

Posted 2010-07-28T17:10:13.037

Reputation: 27 731

Answers

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

Robert Harder

Posted 2010-07-28T17:10:13.037

Reputation: 91

4

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.

Piersg

Posted 2010-07-28T17:10:13.037

Reputation: 41

Oh my, that's quite the command. – nathancahill – 2016-12-16T21:26:01.393

3

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

Dave

Posted 2010-07-28T17:10:13.037

Reputation: 31

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. – None – 2011-02-28T00:10:17.370

You escape it with backslashes: File\ with\ Spaces.txt – msanford – 2011-02-28T02:05:47.733

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

Lee

Posted 2010-07-28T17:10:13.037

Reputation: 31

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 – 2012-09-18T12:36:33.877

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.

Henno

Posted 2010-07-28T17:10:13.037

Reputation: 639

Unfortunately, osxutils is PPC only. – None – 2014-11-28T01:28:46.797

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.

Lri

Posted 2010-07-28T17:10:13.037

Reputation: 34 501

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

jpetersen

Posted 2010-07-28T17:10:13.037

Reputation: 11

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.

JRobert

Posted 2010-07-28T17:10:13.037

Reputation: 6 128

how would you call that from a command-line? – Svish – 2010-07-28T18:32:13.230

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 – 2010-07-28T19:53:05.447