An AppleScript still unwritten: The marvellous MusicMailer!

After repeatedly discovering that my wife and me separately purchased the same songs from the iTunes Store, my mind came up with a solution to share and explore each others iTunes library without the need to set up an expensive home server.

In general my idea is to frequently create and upload a website that contains informations about all local file tracks that are currently available in your iTunes library.

This custom online iTunes catalog (located in a secure private area on .mac or your own webserver) would also include a link or button for every track displayed that, when clicked by a visitor (wife/kid/grandpa), would send an eMail to your Mac with a specially formatted subject containing the Persistent ID of the chosen track.

Once your Mac receives this eMail message, it would trigger a mail rule executing an AppleScript, that parses the subject line for the Persistent ID, checks if the sender is authorized to receive tracks and, if so, sends back the requested file (identified by its unique Persistent ID) track from your hard disk.

Okay, now how to do this in detail? Well…step by step.

1. iTunes Catalog Creation & Upload

As you might know, all track informations of your current iTunes library are also available inside of a large XML file, which can be easily parsed and processed with your favourite programming language. This XML file is located at:

/Users/yourname/Music/iTunes/iTunes Music Library.xml

Following this approach, some developers already created neat and free apps which export your iTunes library to a beautiful webpage (see here or here), which can be uploaded to a webserver to share with visitors of your website what’s in your iTunes.

We would only need an additional link for every listed file track, that - when clicked - creates a specially formatted eMail message containing the Persistent ID of the chosen track. My suggestion would be something simple as follows:


mailto:musicmailer@getfamilymusic.com?subject=Requested%20iTunes%20track:%2015F71E0B334F61F6

The detection of the unique Persistent ID of a track is no problem, as it is listed together with all its other properties in the XML file.

From my programming experience, it would not be very difficult to write a small Python/Ruby/Perl/C++ script that would frequently parse and process the iTunes XML file, export an iTunes catalog in HTML including the above mentioned kind of link for every available file track and upload it onto a webserver (e.g. simply using Python’s built-in FTP library).

2. AppleScript Mail Rule

Apple Mail allows mail rules to execute AppleScripts, which can process the affected eMail messages. So we could easily install a mail rule that watches out for incoming eMail messages containing the phrase “Requested iTunes track” at the beginning of their subject and then executes an AppleScript to further process this message.

The AppleScript attached to the mail rule could simply parse the subject of the message and single out the given Persistent ID to locate the track on your hard disk. Because if you know the Persistent ID of an iTunes track, you can easily gather informations about it using AppleScript:


tell application "iTunes"
	tell playlist 1
		set matchtracks to every track whose persistent ID is "15F71E0B334F61F6"
		if matchtracks is not {} then
			set matchtrack to item 1 of matchtracks
			set trackloc to POSIX path of ((location of matchtrack) as Unicode text)
		end if
	end tell
end tell

-- returns '/Users/martin/Music/iTunes/iTunes Music/Yael Naïm/Yael Naïm/03 New Soul.m4a'

As already shown in the above AppleScript snippet, it is easily possible to retrieve the location of a file track by its Persistent ID. So the AppleScript could just create a new eMail message with the song attached and send it back to the sender of the initial request. Or, if the file could not be found, it would inform the sender accordingly.

Of course, at first it should definitely match the sender’s eMail address against a list of authorized family members to ensure that songs are not accidentally send to complete strangers.

Unfortunately I am currently quite busy with other projects, so if you are in search for an interesting programming challenge: Build it and send me a copy soon :lol: