09/05/12 14:17:00

Set Color Labels from Alfred and Terminal

I’m getting more into using Color Labels for my work. Since I think labeling should be possible from anywhere I searched for a way to add Labels from the command line and Alfred as well.

Alfred

Alfred is quite an easy task. A couple new extensions, plus some icons that can be called from the Actions do the job quite well.
I made one extension for every color and one to remove the label. The extensions can “toggle” (label/unlabel) the color as well.

on alfred_script(q)

    set q to (POSIX file q) as alias

    tell application "Finder"

        if label index of q = 0 then
            set label index of q to 4
        else
            set label index of q to 0
        end if

end tell


end alfred_script

 

Download: https://www.box.com/s/av7vj4ob6joeufr4a0y3

Command Line

The command line option is a bit trickier, because OS X doesn’t have a CLI utility to set color labels directly. A neat script added to your .profile should do the trick though. I made some changes, since I don’t like typing label 4 file.txt to label an item. label yellow file.txt seems more naturaly, isn’t it?

Note that you can remove a color label by typing “remove”, “none” or “blank”, e.g. label none file.txt or label remove file.txt.

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