Mac OS X,  Mac OS X Server,  Mass Deployment

Command Line Finder Tags

Neat. So you can associate a file with a tag by dragging it into the appropriate TAGS section of a sidebar. You can tag a file while you’re saving it. You can also

Screen Shot 2013-10-23 at 8.01.35 PMTags are kept in Extended Attributes. In com.apple.metadata:_kMDItemUserTags to be exact.

xattr /Users/krypted/Desktop/Test.pdf

The output of xattr with no options is as follows:

com.apple.FinderInfo
com.apple.metadata:_kMDItemUserTags
com.apple.quarantine

Or use -l to get a more detailed output:

xattr -l /Users/krypted/Desktop/Test.pdf

The output of which would include the tag:

com.apple.FinderInfo:
00000000  00 00 00 00 00 00 00 00 00 1C 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000020
com.apple.metadata:_kMDItemUserTags:
00000000  62 70 6C 69 73 74 30 30 A1 01 55 52 65 64 0A 36  |bplist00..URed.6|
00000010  08 0A 00 00 00 00 00 00 01 01 00 00 00 00 00 00  |................|
00000020  00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000030  00 10                                            |..|
00000032
com.apple.quarantine: 0041;520d0314;Safari;

This output is a bit not awesome. So

mdls -name kMDItemUserTags /Users/krypted/Desktop/Test.pdf

The output is the kMDItemUserTags array with a list of objects in the array:

kMDItemUserTags = (
Red
)

The above output is much more easily parsed and managed; however, the objects need to have been indexed by Spotlight. You can also use mdfind to look at user tags with kMDItemUserTags to see a list of tagged content. Or, you can constrain the output using == and a quoted parameter:

mdfind "kMDItemUserTags == Red"

The output is a list of all files with that tag. Finally, you can

xattr -c /Users/krypted/Desktop/Test.pdf