Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tagging files with colors in OS X Finder from shell scripts

One can tag files and folders with a color in the Mac OS X Finder. Is there a way to do this from a shell script?

like image 764
Ralf Ebert Avatar asked Mar 12 '10 20:03

Ralf Ebert


2 Answers

This shell script takes the file or folder name as its first argument and the label index (0 for no label, 1 for red, ..., 7 for gray) as its second argument.

#!/bin/sh
osascript -e "tell application \"Finder\" to set label index of alias POSIX file \"`cd -P -- "$(dirname -- "$1")" && printf '%s\n' "$(pwd -P)/$(basename -- "$1")"`\" to $2"

More directly, if $filename is a shell variable with the absolute path name of the file or folder to be labeled and $label is a shell variable with the label index number,

osascript -e "tell application \"Finder\" to set label index of alias POSIX file \"$filename\" to $label"

is a shell command to assign the label to the file or folder.

like image 88
Isaac Avatar answered Sep 19 '22 14:09

Isaac


Here's a quick python script I wrote:

https://github.com/danthedeckie/finder_colors

which sets the colours of folders and files from the commandline.

Usage:

finder_colors.py red /Users/daniel/src

sets the /Users/daniel/src directory to be red.

finder_colors.py /Users/daniel/src

returns the colour (in this case now, 'red'). If you're writing a python script, you can import finder_colors as a module, and use it directly (finder_colors.get(...), and finder_colors.set(...).

like image 41
Daniel Avatar answered Sep 22 '22 14:09

Daniel