Using information found in the article listed in the Comprehensive summary of Mac Metadata hint, I was able to find a way to write metadata from the CLI, making it possible for shell scripts to modify files in a way that is visible to the Finder, and therefor in Smart Folders and other search results.
Using xattr on the CLI you can write arbitrary metadata to files. This is useful for other CLI scripts because you can search for these arbitrary attributes using mdls. However, if you want to set metadata that is easily searchable in applications like Finder, you have to set the values for existing attributes.
Looking through the output of mdimport -X, I located the StarRating attribute as a valuable piece of information to set values for. To set this value for a file, issue the following command:
xattr -w "com.apple.metadata:kMDItemStarRating" "10" filename
This filename would now be searchable in Finder using the Rating attribute, and a smart folder could then be created.
To make it easier to integrate and do large operations, I wrote the following script:
It's worth noting that the Finder will not find any rating that is not an integer, but mdfind will even find strings.
[crarko adds: I haven't tested this one. The script looks OK, but let me know if Geeklog mangled any of the special characters and I will fix that.]
Using xattr on the CLI you can write arbitrary metadata to files. This is useful for other CLI scripts because you can search for these arbitrary attributes using mdls. However, if you want to set metadata that is easily searchable in applications like Finder, you have to set the values for existing attributes.
Looking through the output of mdimport -X, I located the StarRating attribute as a valuable piece of information to set values for. To set this value for a file, issue the following command:
xattr -w "com.apple.metadata:kMDItemStarRating" "10" filename
This filename would now be searchable in Finder using the Rating attribute, and a smart folder could then be created.
To make it easier to integrate and do large operations, I wrote the following script:
#!/bin/bash
## Check for existence of xattr, fail if not
command -v xattr &>/dev/null || { echo "xattr is required, are you using OS X?" >&2 ; exit 1 ; }
## Check for at least two arguments, print usage if fail
if [ $# -lt 2 ] ; then
echo "This script sets the star rating of a file in OS X and requires at least two arguments:";
echo "Usage: $0 rating file [file] [file] ...";
exit;
fi
rating=${1}; # first arg is rating
shift; # remove first arg
files=$@; # remaining args are files
xattr -w "com.apple.metadata:kMDItemStarRating" "${rating}" $@
[crarko adds: I haven't tested this one. The script looks OK, but let me know if Geeklog mangled any of the special characters and I will fix that.]
•
[3,623 views]

