Is there any text length limitations for how long text can be put to xattr? I plan to put my AppleScripts source code to script files so Spotlight can find it. Using Finder Comments isn't an option due to the volume of changes I will be writing.

do shell script "xattr -w com.apple.metadata:kmditemmusicalgenre " & source_code & " " & quoted form of script_path
  • 1
    Interesting mix of tools. It might be challenging to tease apart what the limit on AppleScript is, versus the interface to "do shell script" versus the shell you have chosen as a default shell. Looking at the xattr source code headers, you appear to have constants for 127 character length for any name and a 64 Meg limit for the command itself, but my guess is you'll have problems sooner than writing (64 Meg-1 byte) – bmike Apr 14 '13 at 15:58

As bmike mentioned, xattr.h has a constant called XATTR_MAXSIZE that is set to 64 MiB:

/* Maximum extended attribute size supported by VFS */
#define XATTR_MAXSIZE       (64 * 1024 * 1024)

VFS means virtual file system.

From http://en.wikipedia.org/wiki/Extended_file_attributes#Mac_OS_X:

Although the named forks in HFS+ support arbitrarily large amounts of data through extents, the OS support for extended attributes only supports inline attributes, limiting their size to that which can fit within a single B*-tree node.

I don't know if that is accurate or how to see that size. You'll run into the maximum argument length first though:

$ getconf ARG_MAX
$ touch a.txt
$ xattr -w someattribute "$(dd if=/dev/random bs=140000 count=1)" a.txt
1+0 records in
1+0 records out
140000 bytes transferred in 0.011996 secs (11670527 bytes/sec)
xattr: [Errno 7] Argument list too long: 'a.txt'

I couldn't figure out any way to get input from STDIN or any other workarounds for it.

You can also use custom attribute names:

$ xattr -w com.apple.metadata:MyAttribute gfdylvyieo a.txt
$ mdls -n MyAttribute a.txt
MyAttribute = "gfdylvyieo"
$ mdfind gfdylvyieo
  • Thanks Lauri Ranta!!! If i sum this up, i can actually create own custom attribute names and Spotlight sees it. And max length in characters is unknown. My longest script is 24000 characters long (count characters in source_code), putting this much info to xattr is probably ok? I have no idea what this means but it's probably nothing serious: > I couldn't figure out any way to get input from STDIN or any other workarounds for it. But this sound bit scary. Does this mean that when i save script again in AppleScript Editor, it destroys these xattrs: > Since MacOS 10.6, user space extended attrib – user47456 Apr 15 '13 at 13:48
  • The maximal length for the name of the attribute is 127 characters, everything longer will result in an error message. – nohillside Apr 15 '13 at 13:55
  • @LucasFan Yeah, the maximum length is about 130000 characters. I just meant that like cat b.txt | xattr -w someattribute - a.txt or xattr -w someattribute <(cat b.txt) a.txt didn't work. Saving files shouldn't remove custom extended attributes. – Lri Apr 15 '13 at 14:35

Just to add a second more pragmatic answer. It's usually better to get an app that can index the files you want indexed rather than stuff duplicate data into the resource forks / metadata.

The question above shows how to use mdls to pick apart the file classification of the data files you want to index and then how to get an importer into the spotlight system to handle that file type reported so that you get that file type within the mdimport -L listing so you are indexing the contents of all the files you wish to include.

You must log in to answer this question.

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