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

157

Solution 1

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

Solution 2

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

Solution 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

Solution 4

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

Solution 5

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
Share:
157

Related videos on Youtube

agt
Author by

agt

Updated on September 17, 2022

Comments

  • agt
    agt over 1 year

    Exercise 15 in Learning Python the Hard Way is about starting to play with the files which are read in the script using two methods:

    • through argv, and
    • through raw_input.

    Here is the script:

    from sys import argv
    
    script, filename = argv
    
    txt = open(filename)
    
    print "Here's your file %r:" % filename
    print txt.read()
    
    print "I'll also ask you to type it again:"
    file_again = raw_input("> ")
    
    txt_again = open(file_again)
    
    print txt_again.read()
    

    The author proposes some problems. The fifth one is try to figure out:

    why one method of getting the filename is better than the other.
    

    I would like to know if there is a real advantage in using one rather than the other.

  • Svish
    Svish over 13 years
    how would you call that from a command-line?
  • JRobert
    JRobert over 13 years
    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...
  • adriaan
    adriaan about 13 years
    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.
  • msanford
    msanford about 13 years
    You escape it with backslashes: File\ with\ Spaces.txt
  • JoshP
    JoshP over 11 years
    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.
  • Admin
    Admin over 9 years
    Unfortunately, osxutils is PPC only.
  • nathancahill
    nathancahill over 7 years
    Oh my, that's quite the command.
  • Matt Sephton
    Matt Sephton over 2 years
    Sadly the -x option has been deprecated, so this no longer works (I'm using Big Sur)
  • Piersg
    Piersg over 2 years
    '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