Extended Attributes

From OS X Scientific Computing

Jump to: navigation, search

Contents

HFS+ and metadata

One of the advantages of the HFS+ (and the zfs) is its support for arbitrarily extensible metadata attributes. Put simply, you can associate pretty much any information that suits you by using extended attributes.

A subset of possible attributes includes those used by SpotLight and its command-line counterpart, mdfind, to do quick searches.

The unix command xattr is our key to unlocking this potential.


A simple example

Let's start with a simple example. I downloaded a pdf from Science to my desktop to read. As is typical, it gets a less than descriptive filename:

% ls -lF *.pdf
-rw-r--r--@ 1 wgscott  staff   222K Feb 27 17:08 1229.pdf

The @ tells you that the file, despite just having been downloaded, has some form of extended attribute associated with it. Let's have a look:

% xattr -l 1229.pdf

com.apple.metadata:kMDItemWhereFroms:
0000   62 70 6C 69 73 74 30 30 A1 01 5F 10 37 68 74 74    bplist00.._.7htt
0010   70 3A 2F 2F 77 77 77 2E 73 63 69 65 6E 63 65 6D    p://www.sciencem
0020   61 67 2E 6F 72 67 2F 63 67 69 2F 72 65 70 72 69    ag.org/cgi/repri
0030   6E 74 2F 33 32 33 2F 35 39 31 38 2F 31 32 32 39    nt/323/5918/1229
0040   2E 70 64 66 08 0A 00 00 00 00 00 00 01 01 00 00    .pdf............
0050   00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00    ................
0060   00 00 00 00 00 44                                  .....D

com.apple.quarantine: 0000;49a88e87;Safari.app;|com.apple.Safari

In addition to a fair amount of jibberish, we find the URL for the pdf from Science, and the fact is was downloaded by Safari.

We can make this more useful. For example, I can add in some attribute that tells me who the senior author is:

% xattr -w "Senior Author" "Gerald F. Joyce" 1229.pdf 

zsh-% xattr -l 1229.pdf        
                          
Senior Author: Gerald F. Joyce
com.apple.metadata:kMDItemWhereFroms:
0000   62 70 6C 69 73 74 30 30 A1 01 5F 10 37 68 74 74    bplist00.._.7htt...

So you can see how that might be useful.

Similarly, I can delete information:

% xattr -d "Senior Author" 1229.pdf 
% xattr -l 1229.pdf      
          
com.apple.metadata:kMDItemWhereFroms:
0000   62 70 6C 69 73 74 30 30 A1 01 5F 10 37 68 74 74    bplist00.._.7htt...
 
% xattr -d "com.apple.metadata:kMDItemWhereFroms" 1229.pdf 
% xattr -l 1229.pdf    
<prints nothing>

Use with Spotlight and mdfind

Unfortunately, Spotlight and mdfind won't make use of our "Senior Author" extended attribute that we just made up. We are restricted to the kMDItem types that Spotlight is designed to make use of. mdls comes to our rescue:

% mdls 1229.pdf | grep Author
<prints nothing>
% xattr -w "com.apple.metadata:kMDItemAuthors" "Joyce" 1229.pdf    
% mdls 1229.pdf | grep Author
kMDItemAuthors                 = "Joyce"

Now if you use spotlight or the Finder's "find" GUI, you can find this file by looking for an author named Joyce.


A convenient shell script for changing Spotlight metadata on a file

I wrote a shell script called mdattr

% mdattr

 mdattr is a function that can create, delete or revise Apple-style metadata attributes
 of the form kMDItemAttributeName and assign each to a value of AttributeValue 

 eg:  kMDItemKind  = "Document"  


 Usage:   mdattr   <AttributeName>   <AttributeValue>   filename(s)  

          mdattr  -d  <AttributeName> filename(s)  

Here is a link to the zsh shell script mdattr and its associated _mdattr zsh completion function.

It works like this:

% mdattr -d Authors 1229.pdf 

 The metadata of the file 1229.pdf have been modified as follows: 

% mdls 1229.pdf | grep Author
% mdattr Authors Joyce 1229.pdf 

 The metadata of the file 1229.pdf have been modified as follows: 
kMDItemAuthors                 = "Joyce"

Personal tools