쉘 스크립트에서 OS X Finder에서 색상으로 파일 태그
-
19-09-2019 - |
문제
Mac OS X Finder에서 색상으로 파일과 폴더를 태그 할 수 있습니다. 쉘 스크립트에서 이것을 할 수있는 방법이 있습니까?
해결책
이 쉘 스크립트는 파일 또는 폴더 이름을 첫 번째 인수로, 레이블 인덱스 (레이블의 경우 0, 빨간색의 경우 1, 7의 경우 7)를 두 번째 인수로 간주합니다.
#!/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"
보다 직접적으로 $ filename이 라벨링 할 파일 또는 폴더의 절대 경로 이름이있는 쉘 변수 인 경우, $ label은 레이블 인덱스 번호가있는 쉘 변수입니다.
osascript -e "tell application \"Finder\" to set label index of alias POSIX file \"$filename\" to $label"
레이블을 파일 또는 폴더에 할당하는 쉘 명령입니다.
다른 팁
다음은 내가 쓴 빠른 파이썬 스크립트입니다.
https://github.com/danthedeckie/finder_colors
CommandLine에서 폴더와 파일의 색상을 설정합니다.
용법:
finder_colors.py red /Users/daniel/src
/user/daniel/src 디렉토리를 빨간색으로 설정합니다.
finder_colors.py /Users/daniel/src
색상을 반환합니다 (이 경우 '빨간색'). Python 스크립트를 작성하는 경우 Finder_Colors를 모듈로 가져 와서 직접 사용할 수 있습니다 (Finder_Colors.get (...) 및 Finder_Colors.set (...).
여기의 응답과 참조 게시물을 기반으로 다음 기능을 만들어 내에게 추가했습니다. ~/.bash_profile
파일:
# 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
}
이를 수행하는 한 가지 추악한 방법은 다음과 같습니다.
exec osascript <<\EOF
tell app "Finder"
-- [...]
-- selecting the file
-- [...]
-- 4 is Blue
set label index of thisItem to 4
end tell
기본적으로 파인더를 사용하여 색상을 설정하는 사과 스크립트를 시작합니다.
힌트를 얻었습니다.
(색깔) http://www.macosxhints.com/article.php?story=20070602122413306
(껍데기) http://www.macosxhints.com/article.php?story=20040617170055379
또한 명령 줄 도구 'setlabel'도 있습니다. osxutils 패키지. 사과 스크립트가 필요하지 않거나 파인더가 실행 중입니다.
이것은 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
나는 같은 경고를 받았기 때문에 stderr를 리디렉션하고 있습니다 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
10.8. OsAscript가 마지막 표현식의 값을 인쇄하기 때문에 STDOUT가 리디렉션됩니다.