7

I am looking for a command line utility (I need to use it in a script) that can set Spotlight metadata to files.

2

I don't think there's a way to -- the Spotlight indexes are generated by metadata importers that scan files and figure out their properties. If the relevant importer doesn't detect some property, then as far as Spotlight is concerned it doesn't exist.

Now, it might be possible to change the actual file in such a way as to add properties to its index entry. This page claims you can add extended attributes starting with "com.apple.metadata:" and they'll be added to the spotlight entry for the file, but I couldn't get it to work. The SpotMeta project extends the import system to add extended attributes to the spotlight database, but only works on OS X v10.4. Not an actual solution, but that's as close as I could find...

  • Do you suppose that a database (to temporarily store metadata) combined with a spotlight importer (to pull the metadata from the previous DB) would work? I need to add metadata to simple files (jpeg, txt, rtf etc) just like Safari adds the "Downloaded from URL" metadata to every file I download from the interwebs. – Kostas Jan 16 '11 at 19:03
  • 1
    I don't think so -- if I understand right, it only uses one importer per file type, so your importer would have to replace the regular importer(s), meaning spotlight wouldn't see anything except your info (that's why SpotMeta wasn't an importer, but a mod of spotlight itself). BTW, apparently you can add a finder spotlight comment with xattr -w com.apple.metadata:kMDItemFinderComment "commenthere" filename and it'll get indexed as the kMDItemFinderComment attribute. – Gordon Davisson Jan 16 '11 at 20:21
4

You can always use the command line tool xattr, which lists/reads/writes/erases the filesystem's extended attributes of a file.

That's what spotlight uses to build it's index.

Note that spotlight information keys are prefixed with com.apple.metadata:

As quick example, to change the display name on spotlight of a file:

xattr -w com.apple.metadata:kMDItemDisplayName MyNewFilename.txt ActualFile.txt

to access xattr help, type on t:

xattr -h
  • Still works on El Capitan. Some stock applications like Safari, don't accept writes, though. – Kevin Suttle Jun 16 '16 at 21:55
1

Here is a Python function I use to write Finder Comments to a file using the system shell. They show up in Spotlight, but not in the Get Info box...

def writexattrs(F,TagList):
    import subprocess
    """ writexattrs(F,TagList):
    Writes the list of tags to xattr field of file named F
    """
    plistFront = '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array>'
    plistEnd = '</array></plist>'
    plistTagString = ''
    for Tag in TagList:
        plistTagString = plistTagString + '<string>{}</string>'.format(Tag)
    TagText = plistFront + plistTagString + plistEnd

    WhichAttribute = "com.apple.metadata:kMDItemFinderComment"
    # Other attributes you might want to try: ["kOMUserTags","kMDItemOMUserTags","_kMDItemUserTags","kMDItemkeywords"]
    XattrCommand = 'xattr -w {0} \'{1}\' "{2}"'.format(WhichAttribute,TagText.encode("utf8"),F)
    # optional, print command format to check:
    # print XattrCommand
    ProcString = subprocess.check_output(XattrCommand, stderr=subprocess.STDOUT,shell=True) 
    return ProcString
-1

If you have installed Apple Developer Tools (usually appears under /Developer/Tools), then you have access to the SetFile and GetFileInfo commands which both assist in manipulating metadata of files.

Additionally, I found a set of command line utilities called osxutils that may prove useful with additional requirements that come up while writing your scripts.

  • 2
    SetFile and GetFileInfo do not interact with the Spotlight metadata but with the filesystem metadata. That's why mdls does not return the same as GetFileInfo ( pastie.org/private/w2pzgheje9afyguadhj5fq ). osxutils does not seem to provide the functionality I require and has been abandoned since 2005 (about the same time Spotlight came out). – Kostas Jan 3 '11 at 0:55
  • I see. I didn't find any resources that indicated too significant a difference. I will continue my search and update my answer with more on point content. My apologies for the incorrect information. – Ryan Wersal Jan 3 '11 at 1:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .