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?
+2
A:
One ugly way to do this would be:
exec osascript <<\EOF
tell app "Finder"
-- [...]
-- selecting the file
-- [...]
-- 4 is Blue
set label index of thisItem to 4
end tell
Basically launching an applescript that uses finder to set the color.
I got the hints from:
(Color) http://www.macosxhints.com/article.php?story=20070602122413306
(Shell) http://www.macosxhints.com/article.php?story=20040617170055379
anton
2010-03-12 20:41:50
+4
A:
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.
Isaac
2010-03-12 21:02:58
This will fail if the filename contains double quotes or ends with a backslash.
Kevin Reid
2010-03-14 00:27:34
@Kevin: Any solution to that?
Svish
2010-07-28 18:37:18
@Kevin: Also... why would you have a filename containing double quotes? I thought that was invalid... or maybe just in Windows...
Svish
2010-07-28 18:43:38
The only characters not allowed in filenames are NUL (U+0000) and the path separator (which is either "/" or ":" depending on which API you look through). A safe way to pass strings to AppleScript is to to give command-line args to `osascript` (that is, `osascript -e <script> <arg>` and then retrieve them using an `on run theArguments ... end run` handler inside the script.
Kevin Reid
2010-07-30 01:36:21