Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

In trying to get a list of all music files from a particular album using mdfind I see that the files aren't necessarily returned in any order. I'd prefer album/track order if possible, but can't find a way to provide the sort order on the command line. The current invocation is something like:

mdfind -onlyin "$MUSICROOT" -literal "$MDQUERY"

where:

$MUSICROOT is ~/Music/iTunes
$MDQUERY is "kMDItemContentTypeTree == 'public.audio' && kMDItemAlbum == '*$1*'c"

Ideally I'd want to add something like the following to the query:

ORDER BY kMDItemAlbum, kMDItemAudioTrackNumber
share|improve this question

2 Answers 2

up vote 0 down vote accepted

At the risk of stating the obvious, 'sort'.

e.g. mdfind -onlyin "$MUSICROOT" -literal "$MDQUERY" | sort

will sort the output alphabetically, giving you Artist, Title, Track. (it won't do the cool iTunes trick of ignoring 'The').

The sort command has lots of options, including sorting on particular fields if you want more control.

Edit: The below shell script function will provide the desired sort in the specific case mentioned:

function sort_file()
{
    TMPFILE=`mktemp /tmp/sortable.XXXXXX` || exit 1
    export MDLS="mdls -raw -name kMDItemAlbum -name kMDItemAudioTrackNumber"
    export TR="tr \"\0\" \"\;\""
    awk '{system( "$MDLS \"" $0 "\" | $TR" ); print "\;"$0; }' $1 > $TMPFILE
    sort -t\; -k 2n -k 1f $TMPFILE | awk -F";" '{ print $3; }' > $1
}
share|improve this answer
    
The problem with piping the output to sort is that mdfind outputs filenames and I want to sort on metadata associated with the files. sort was going to be my backup solution because it will require me to do an mdls for each result pipe that to a file, then sort the file on those fields and output the filename. –  jmhindle Aug 23 '11 at 19:25
    
Sorry, I was going by the examples you gave and thought that their filename equivalents would be sufficient. –  zzz Aug 23 '11 at 20:04
    
I played around with mdfind, mdls, awk and some other Unix tools last night but couldn't get it to work correctly and consistently (mainly because mdfind/mdls output isn't very Unix friendly in terms of parseability) :-( –  patrix Aug 24 '11 at 17:05
    
I'm going to go with the sort solution because I don't think mdfind allows a sort. If I used the spotlight API I could provide a sort callback but it would still generate the unsorted list of data first. –  jmhindle Aug 24 '11 at 17:21

I used the following shell function to provide the sort:

function sort_file()
{
    TMPFILE=`mktemp /tmp/sortable.XXXXXX` || exit 1
    export MDLS="mdls -raw -name kMDItemAlbum -name kMDItemAudioTrackNumber"
    export TR="tr \"\0\" \"\;\""
    awk '{system( "$MDLS \"" $0 "\" | $TR" ); print "\;"$0; }' $1 > $TMPFILE
    sort -t\; -k 2n -k 1f $TMPFILE | awk -F";" '{ print $3; }' > $1
}

META NOTE: I would have put this in a comment in the above answer, but couldn't get it to format correctly.

share|improve this answer
    
I believe you can edit the previous answer to add this great script - it will get approved once someone else reviews it. Nice improvement - far too much for a comment! –  bmike Aug 24 '11 at 18:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.