Where does Spotlight store its metadata index?

7

5

Where does Mac OS X store Spotlight comments associated with a file? Is it stored as HFS+ file metadata, or is the information kept by the OS somewhere else?

Lorin Hochstein

Posted 2009-12-11T22:01:38.250

Reputation: 3 037

Answers

7

The metadata is inside a hidden .Spotlight-V100 folder at the root of the indexed volume.

John T

Posted 2009-12-11T22:01:38.250

Reputation: 149 037

5

The values of the kMDItemFinderComment attributes that get stored in metadata store directories like /.Spotlight-V100/ depend on the com.apple.metadata:kMDItemFinderComment extended attributes.

Finder also stores the Spotlight comments of all items in a folder in a .DS_Store file.

You can print Spotlight comments with mdls or xattr:

mdls -n kMDItemFinderComment test.txt

xattr -p com.apple.metadata:kMDItemFinderComment file.txt | xxd -r -p | plutil -convert xml1 -o - - | ruby -rcgi -e 'puts CGI.unescapeHTML(STDIN.read.scan(/<string>(.*)<\/string>/m)[0][0])'

If you use Finder to add a Spotlight comment for a file you don't have write permission to (like some application bundles in /Applications/), the comment won't be saved as an extended attribute, and it won't get picked up by Spotlight. You can open Finder as the superuser or use sudo xattr -w though.

If you delete a .DS_Store file from a folder and quit and reopen Finder, the Spotlight comments of all files in the folder disappear from Finder's information windows. If there are still extended attributes for the Spotlight comments, the comments are seen by Spotlight though.

If you use xattr to add a com.apple.metadata:kMDItemFinderComment extended attribute, the comment gets picked up by Spotligt, but it won't be shown in Finder's information windows.

xattr -w com.apple.metadata:kMDItemFinderComment comment file.txt

If you use AppleScript to change the comment attribute of a file, the comment is also saved in a .DS_Store file and shown in Finder.

osascript -e 'on run {f, c}' -e 'tell app "Finder" to set comment of (POSIX file f as alias) to c' -e end file.txt comment

(There is a warning like CFURLGetFSRef was passed this URL which has no scheme in 10.8 when a relative path is converted to an alias, but you can ignore it.)

Lri

Posted 2009-12-11T22:01:38.250

Reputation: 34 501

Please provide a way to verify this. I was unable to do so. Setup: Create folders foo and foo/bar, use Automator to set Spotlight comment baz on foo/bar. Open foo in Finder, search for baz in it. This finds foo/bar. Then open Terminal, delete foo/.DS_Store (there's no foo/bar/.DS_Store). Relaunch Finder. Open foo in Finder, search for baz: Still finds foo/bar. – Daniel Beck – 2011-03-11T17:40:40.963

This works for me! Is there a way to write them? I've tried writing to the plist as well as an ascii-encoded hex string and neither sticks. I can write keywords that Spotlight finds using xattr -w com.apple.metadata:_kMDItemUserTags Maybe I should write this as a question of its own... – beroe – 2013-08-29T17:50:01.887

1@beroe I edited the answer. – Lri – 2013-08-30T07:09:07.300

0

If you're trying to edit the spotlight comments on a file, you can see/modify them under the Get Info (command + I) window for the file.

Kaji

Posted 2009-12-11T22:01:38.250

Reputation: 239