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

4 Answers 4


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, 2011 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. Jan 16, 2011 at 20:21
  • @GordonDavisson thanks for your clarifying comment "Only one importer per file type". That makes clear that if I wanted an additional parameter like "Compression Ratio" for lossy compressed images (JPEG, WEBP, etc) it is not possible to create an additional MDimporter handling those public image file types which runs in addition to /System/Library/Spotlight/Image.mdimporter. If we want that the only possibility is to request from Apple to add a new attribute to the central Image.mdimporter.
    – porg
    Nov 1, 2022 at 9:30
  • @porg Keep in mind that I'm not an expert on this, and it's possible something has changed in the last decade of updates. I'd take a look at the current docs and see if there's any new option to do this. Nov 1, 2022 at 9:43
  • Spotlight on search: How to diagnose and fix problems is a good article as a whole, but neither the article nor the included diagram answers the particular question of how "more than one importer per UTI" is handled.
    – porg
    Nov 1, 2022 at 10:28

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. Jun 16, 2016 at 21:55

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

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, 2011 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. Jan 3, 2011 at 1:31

You must log in to answer this question.

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