2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

macでファイルに色タグをターミナル(コマンド)でつける

Posted at

macでターミナル(つまりコマンド)からファイルへ色タグをつけたいときのコマンド

xattrコマンドを使う

このようなコマンドがあるそうな。
これを使えば、mac特有の拡張属性とかいろいろ付けたり外したりできる。

ひとまずhelp打ってみる。

$ xattr -h
usage: xattr [-l] [-r] [-s] [-v] [-x] file [file ...]
       xattr -p [-l] [-r] [-s] [-v] [-x] attr_name file [file ...]
       xattr -w [-r] [-s] [-x] attr_name attr_value file [file ...]
       xattr -d [-r] [-s] attr_name file [file ...]
       xattr -c [-r] [-s] file [file ...]

The first form lists the names of all xattrs on the given file(s).
The second form (-p) prints the value of the xattr attr_name.
The third form (-w) sets the value of the xattr attr_name to the string attr_value.
The fourth form (-d) deletes the xattr attr_name.
The fifth form (-c) deletes (clears) all xattrs.

options:
  -h: print this help
  -l: print long format (attr_name: attr_value and hex output has offsets and
      ascii representation)
  -r: act recursively
  -s: act on the symbolic link itself rather than what the link points to
  -v: also print filename (automatic with -r and with multiple files)
  -x: attr_value is represented as a hex string for input and output

ということで、
p : 拡張属性の表示
w :     の付与
d :     の削除(指定された属性)
c :     のクリア(全属性)
ベースはこれで、追加オプションもちらほら。

一度、適当なファイルに赤色のタグ付けてxattr [file]を実行。

$ xattr tagtest 
com.apple.FinderInfo
com.apple.metadata:_kMDItemUserTags

2つの属性が付与されていた。
上の方のFinderInfoが色タグ(1つ)の情報で、
下の_kMDItemUserTagsはユーザー定義タグ情報とかだそうな(あと複数タグの情報とかもこちらっぽい)
xattr -p -x com.apple.FinderInfo [file]とコマンドを打って、内容確認

$ xattr -p -x com.apple.FinderInfo tagtest 
00 00 00 00 00 00 00 00 00 0C 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

このHexの0Cの部分がタグの色の情報だそうな。(0Cは赤)

コマンドで色タグを付ける場合は、

$ xattr -wx com.apple.FinderInfo "00 00 00 00 00 00 00 00 00 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" [file]

Hexが長い…
で実際に赤色のタグをターミナルからつけることができた。

色情報

none = 0, 1
grey = 2, 3
green = 4, 5
purple = 6, 7
blue = 8, 9
yellow = A, B
red = C, D
orange = E, F
だそうな。
(拾った情報で全部は検証していない)

ユーザータグとか複数色タグをつける方法

XMLとか作ったりしていけるみたい。
未検証

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?