Is there a maximum length of metadata that can be written in one xattr command? 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 " &amp; source_code &amp; " " &amp; quoted form of script_path
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
262144
$ 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
/private/tmp/a.txt