Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Set StarRating from CLI Apps
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:
#!/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}" $@
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.]
    •    
  • Currently 4.00 / 5
  You rated: 5 / 5 (8 votes cast)
 
[3,626 views]  

Set StarRating from CLI | 2 comments | Create New Account
Click here to return to the 'Set StarRating from CLI ' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Set StarRating from CLI
Authored by: patpro on Mar 16, '11 04:49:57AM
Awesome. I'm using Bridge (CS3) to manage my picture collection. I rate picts to sort them, but this rating is not recorded is the Spotlight Metadata. Thanks to you, I've been able to create a small script to export the rating from the Adobe XMP file to picture's Spotlight metadata:
for XML in $(find . -name IMG_*xmp); do 
rating=$(awk '/xap:Rating/ {gsub(/ *<[^>]*>/,"",$0); print $0}' $XML)
[ -f ${XML/xmp/CR2} ] && xattr -w "com.apple.metadata:kMDItemStarRating" $rating ${XML/xmp/CR2}
done
---
http://www.patpro.net/


[ Reply to This | # ]
Set StarRating from CLI
Authored by: patpro on Mar 20, '11 12:22:21PM

After playing around with xattr and kMDItemStarRating, I've discovered that the Finder does not recognize properly the content of this property as an integer. You just can"t make a filter to search for pictures with a rating that is "more than" or "less than". Equality test works, but not less or more.
That's strange, and I can't find a good reason for that.

---
http://www.patpro.net/



[ Reply to This | # ]