端末コマンドを介してマーベリックスのファイルまたはフォルダにタグを付けることは可能ですか?
端末コマンドを介してマーベリックスのファイルまたはフォルダにタグを付けることは可能ですか?
回答:
xattrを使用できます。これにより、タグがfile1からfile2にコピーされます。
xattr -wx com.apple.metadata:_kMDItemUserTags "$(xattr -px com.apple.metadata:_kMDItemUserTags file1)" file2;xattr -wx com.apple.FinderInfo "$(xattr -px com.apple.FinderInfo file1)" file2
タグは、文字列の単一の配列としてプロパティリストに保存されます。
$ xattr -p com.apple.metadata:_kMDItemUserTags file3|xxd -r -p|plutil -convert xml1 - -o -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Red
6</string>
    <string>new tag</string>
    <string>Orange
7</string>
    <string>Yellow
5</string>
    <string>Green
2</string>
    <string>Blue
4</string>
    <string>Purple
3</string>
    <string>Gray
1</string>
</array>
</plist>
com.apple.FinderInfoのkColorフラグが設定されていない場合、Finderは色の円を表示しません。kColorフラグがオレンジに設定されていて、ファイルに赤いタグが付いている場合、Finderは赤い円とオレンジの円の両方を表示します。AppleScriptでkColorフラグを設定できます。
xattr -w com.apple.metadata:_kMDItemUserTags '("Red\n6","new tag")' ~/desktop/file4;osascript -e 'on run {a}' -e 'tell app "Finder" to set label index of (POSIX file a as alias) to item 1 of {2, 1, 3, 6, 4, 5, 7}' -e end ~/desktop/file4
xattr -p com.apple.FinderInfo file|head -n1|cut -c28-29kColorフラグに使用されるビットの値を出力します。赤はC、オレンジはE、黄色はA、緑は4、青は8、マゼンタは6、灰色は2です。値に1を加えるフラグはOS Xでは使用されません。
編集:タグを使用することもできます:
tag -l file # list
tag -a tag1 file # add
tag -s red,blue file # set
tag -r \* file # remove all tags
tag -f green # find all files with the green tag
tag -f \* # find all files with tags
tag -m red * # match (print files in * that have the red tag)
タグはbrew install tagまたはでインストールできますsudo port install tag。
$ tag -h
tag - A tool for manipulating and querying file tags.
  usage:
    tag -a | --add <tags> <file>...     Add tags to file
    tag -r | --remove <tags> <file>...  Remove tags from file
    tag -s | --set <tags> <file>...     Set tags on file
    tag -m | --match <tags> <file>...   Display files with matching tags
    tag -l | --list <file>...           List the tags on file
    tag -f | --find <tags>              Find all files with tags
  <tags> is a comma-separated list of tag names; use * to match/find any tag.
  additional options:
        -v | --version      Display version
        -h | --help         Display this help
        -n | --name         Turn on filename display in output (default)
        -N | --no-name      Turn off filename display in output (list)
        -t | --tags         Turn on tags display in output (find, match)
        -T | --no-tags      Turn off tags display in output (list)
        -g | --garrulous    Display tags each on own line (list, find, match)
        -G | --no-garrulous Display tags comma-separated after filename (default)
        -H | --home         Find tagged files only in user home directory
        -L | --local        Find tagged files only in home + local filesystems (default)
        -R | --network      Find tagged files in home + local + network filesystems
        -0 | --nul          Terminate lines with NUL (\0) for use with xargs -0
純粋なbashコマンドを介してタグを操作することが可能です。サードパーティの「タグ」ユーティリティは必要ありません。
このコマンドは、ファイル($ src)のすべてのタグをリストします。
xattr -px com.apple.metadata:_kMDItemUserTags "$src" | \
    xxd -r -p - - | plutil -convert json -o - - | sed 's/[][]//g' | tr ',' '\n'
そして、タグ($ newtag)をファイル($ src)に追加する方法は次のとおりです。
xattr -wx com.apple.metadata:_kMDItemUserTags \
    "$(xattr -px com.apple.metadata:_kMDItemUserTags "$src" | \
    xxd -r -p - - | plutil -convert json -o - - | sed 's/[][]//g' | tr ',' '\n' | \
    (cat -; echo \"$newtag\") | sort -u | grep . | tr '\n' ',' | sed 's/,$//' | \
    sed 's/\(.*\)/[\1]/' | plutil -convert binary1 -o - - | xxd -p - -)" "$src"
これは、「タグ」関数をエクスポートする小さなシェルスクリプトです。使用法:
tags <file>
Lists all tags of a file
tags -add <tag> <file>
Adds tag to a file
この機能は、削除もサポートするように簡単に拡張できます。
tags() {
    # tags system explained: http://arstechnica.com/apple/2013/10/os-x-10-9/9/
    local src=$1
    local action="get"
    if [[ $src == "-add" ]]; then
        src=$3
        local newtag=$2
        local action="add"
    fi
    # hex -> bin -> json -> lines
    local hexToLines="xxd -r -p - - | plutil -convert json -o - - | sed 's/[][]//g' | tr ',' '\n'"
    # lines -> json -> bin -> hex
    local linesToHex="tr '\n' ',' | echo [\$(sed 's/,$//')] | plutil -convert binary1 -o - - | xxd -p - -"
    local gettags="xattr -px com.apple.metadata:_kMDItemUserTags \"$src\" 2> /dev/null | $hexToLines | sed 's/.*Property List error.*//'"
    if [[ $action == "get" ]]; then
        sh -c "$gettags"
    else
        local add="(cat -; echo \\\"$newtag\\\") | sort -u"
        local write="xattr -wx com.apple.metadata:_kMDItemUserTags \"\$($gettags | $add | grep . | $linesToHex)\" \"$src\""
        sh -c "$write"
    fi
}
export -f tags
xattr -wxファイルがすでにその上に任意のタグを持っていない場合、コマンドは失敗します。どうすればこれを回避できますか?
                    xattr -px …指定したコマンドを実行して、いずれかのフォルダーのタグを表示すると、次の出力が得られます:("language:Objective-C\n2"改行)"platform:iOS\n4"。  正直なところ、適度に複雑なシェルコードをbash関数にラップする場合は、コミュニティのメンテナンスがしやすいという利点があるtagの作業を複製するだけです。