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 scr开发者_JAVA百科ipt?
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.
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(...).
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
}
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
There's also the command line tool 'setlabel' in the osxutils package. It doesn't require AppleScript or that the Finder be running.
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 application "Finder"
repeat with f in arguments
set f to (posix file (contents of f) as alias)
set label index of f to $n
end repeat
end tell
end
END
I'm redirecting STDERR, because I got warnings like 2012-09-06 13:50:00.965 osascript[45254:707] CFURLGetFSRef was passed this URL which has no scheme (the URL may not work with other CFURL routines): test.txt
on 10.8. STDOUT is redirected because osascript prints the value of the last expression.
精彩评论