Using mdfind to preview all tracks by an artist

Two among OS X’s more underutilised CLI tools are mdls and mdfind.

These both query the detailed metadata in Spotlight’s database (hence the md prefix).  The first command will return all the attributes of a file or directory.  The second will find all files that match a given attribute query string.  O’Reilly’s MacDevCenter has an article detailing advanced use of these commands.

The first example uses Apple’s afplay, but since the AudioFile API seems to be incapable of understanding its own DRM (*.m4p), the second example uses qtplay from MacPorts.

  • Listen to the first 5 seconds of all tracks by Mute Math:
    mdfind "kMDItemAuthors == 'Mute Math'" | while read f; do afplay -s 0 5 "$f"; done
  • Listen to a random track of 148 – 152bpm:
    mdfind "kMDItemTempo >= 148 && kMDItemTempo <= 152" | qtplay -1 -- -

Given the pervasive nature of Spotlight’s metadata, the possibilities are vast.  Hint: SSH and “&”.

Comments are closed.