Tagging files with colors in OS X Finder from shell scripts

六眼飞鱼酱① 提交于 2019-11-27 01:11:23

问题


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?


回答1:


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.




回答2:


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(...).




回答3:


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
}



回答4:


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




回答5:


There's also the command line tool 'setlabel' in the osxutils package. It doesn't require AppleScript or that the Finder be running.




回答6:


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.



来源:https://stackoverflow.com/questions/2435580/tagging-files-with-colors-in-os-x-finder-from-shell-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!