Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

macos - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
' "$(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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...