Using mdls to get metadata from mp3 files

I just discovered the mdls command last night and it goes a long way to solve some scripting issues for me. I have stacks of mp3 cds and dvds (my collection of music is too large for my iPod) that i want to catalog. Using the mdls, I can extract the song title, album, and artist, but the string that is returned from the do shell script contains a lot of extra junk. I have worked around that by reading the string from a unique point, but I wonder if there is a simpler way. For instance: running the following shell script:

do shell script mdls -name kMDItemAuthors “16 Sevivon.mp3”

returns this string:

16 Sevivon.mp3 -------------
kMDItemAuthors = (“Arthur And Friends”)

Of course, all the data I want is Arthur And Friends, so it is easy to read from the parentheses and extract the wanted data, but I am curious if UNIX has a way to return a ‘cleaner’ string.

My second question is probably more of a UNIX issue, but I will throw it out anyway. I want to extract three pieces of data from each mp3:

kMDItemTitle, kMDItemAuthors, and kMDItemAlbum. Can I do this all on a single command line, and return a list of three strings? I am willing to try to write a unique shell script to do it, but I don’t know how and I have no UNIX texts in my possession.

Any suggestions on these, or good UNIX texts would be greatly appreciated.


Try this:

set the_file to (choose file)
set the_title to my get_md(the_file, "kMDItemTitle")
set the_authors to my get_md(the_file, "kMDItemAuthors")
set the_album to my get_md(the_file, "kMDItemAlbum")
return {the_file:(the_file as Unicode text), the_title:the_title, the_authors:the_authors, the_album:the_album}
-->{the_file:"Path:to:Vertigo.m4p", the_title:"Vertigo", the_authors:"U2", the_album:"How to Dismantle an Atomic Bomb"}

on get_md(the_file, the_md_field)
	text 2 thru -2 of (do shell script "mdls -name " & the_md_field & " " & quoted form of POSIX path of the_file & " | grep " & the_md_field & " | awk -F ' = ' '{print $2}'")
end get_md

Was mdls added only with 10.4?



Thanks for the help, this code looks good; I will be working on it tonight and let you know how it goes.

The mdls man page is dated June 2004, so I assume it is pretty new. I read about it in the August 2005 issue of MacWorld in the UNIX column they do every month.

Do you know of any good texts on learning more about UNIX based scripting for OSX?

Craig Smith


The code works perfectly, thank you very much for your help.

Craig Smith