Export iTunes Playlists to MySpace

Since Pilot Training requests for MySpace modifications have been overwhelming as of late, I decided to augment the Hack Pack a little with a geeky yet powerful tool for Mac OS X. Out of the entire Hack Pack, people seem to really be able to get the div overlay technique as well as the RSS reader implementation, but I receive a lot of questions about the MySpace Dev. 4: Restoring music functionality article and the XSPF XML schema. People are able to implement the player and upload the files just fine, however the XML seems to be difficult for people to write.

Because of this, I wrote an AppleScript that needs minimal configuration. Once configured and installed, you can click an “Export to MySpace” button in your iTunes menu bar and have iTunes automatically transcode your files to MP3, upload the files to your FTP server, and generate the XSPF XML playlist for you. In this Pilot Training installment, we’ll set up the script, install the script, and adapt the XSPF player to use the new automated settings. It’s still going to be geek-oriented, and it’s not for the faint of heart or computer ignorant.

Warning This is currently only for Mac OS X. iTunes is not nearly as scriptable on Windows, and I have no idea how to script iTunes on Windows anyway. If anyone does and would like to port this to Windows, please do and I’ll list it here — just e-mail me at eston@hyalineskies.com. This is also my first AppleScript and could probably use some optimisation, so if you’re an AppleScript junkie, I’ll take your suggestions and modifications as well.

Configuring the AppleScript

Once you’ve downloaded the AppleScript, you’ll need to edit the script in the Apple Script Editor. If you’re running OS X 10.3 or later, click here to launch the Script Editor. With the Script editor running, Press Apple+O to open a new script and open the Export to Myspace application included in the ZIP file. The application script is required for iTunes, since iTunes sometimes bugs out on plain AppleScripts.

At the top of the script, there are four variables written in the AppleScript language. AppleScript is very close to plain English and thus is much easier to follow than other languages. The common syntax for a string variable is:

set variable_name to "value"

which will set a variable named variable_name to the value value. The variables at the top of the Export to MySpace script are ftp_server, http_server, ftp_user, and ftp_password. These values will correspond to your FTP server location, your HTTP server path, your FTP username, and FTP password. For ftp_server, input your FTP server path all the way out to the directory where you wish to store the files (e.g., ftp://ftp.myserver.com/domains/mydomain.com/html/myspacemp3/) . For http_server, set the variable to the path that’s accessible via HTTP, such as in your web browser (e.g. http://myserver.com/myspacemp3/). For ftp_user and ftp_password, set the fields to your FTP username and password, respectively. Using the above examples, your code block should look something like this:

set ftp_server to “ftp://ftp.myserver.com/domains/mydomain.com/html/myspacemp3/”
set http_server to “http://myserver.com/myspacemp3/”
set ftp_user to “myusername”
set ftp_password to “myp4ssw0rd”

Make sure that you have this right. If you mess up here, the script will fail in iTunes and you will get a script error. Once you’re sure all your values are right, click the Compile hammer or press Apple+K. If you get an error on compile, chances are you don’t have the syntax correct in the AppleScript — make sure you’re not missing a quotation mark or a to in between the variable name and value. If you don’t get any errors, save the script.

Installing the Script into iTunes

Once the script is done, you’ll want to save the script so that it’s accessible from iTunes. To do so, you’ll need to create a Scripts folder inside of your Library folder. Select the Finder from your OS X Dock, and then click Go > Home in the menu or press Shift+Apple+H. Double click the Library Folder, and then double click the iTunes Folder within the Library Folder. Here, click File > New Folder in the Finder menubar or press Shift+Apple+N to create a new folder. Name the new folder Scripts. Double click the Scripts folder you just created, and drag the Export to MySpace script into the folder. Once you’re done, close the Finder window. If iTunes is open, close and re-open iTunes.

iTunes should recognise your new script and add a small paper scroll icon to the iTunes menubar. This is the Scripts menu. Underneath the Scripts menu, you will now see “Export to MySpace”. If this is the case, you’ve successfully installed the script. If it isn’t there, or you don’t see the Scripts menu, first try restarting your Mac; if it’s still not there, you did something wrong, so go back and look to make sure you did everything right. The path of the folder you made for iTunes should be /Users/username/Library/iTunes/Scripts/, where username is your OS X username.

Using the Export to MySpace script in iTunes

If you already have a playlist called “MySpace” and you don’t want to lose it, rename it to something else, as “MySpace” is the playlist name that the script uses for MySpace songs. If you don’t have a MySpace playlist, go ahead and run the Export to MySpace script. The script will alert you to the fact that you don’t have a MySpace playlist and automatically set one up for you.

Once you’re done setting up your MySpace list, drag a few songs over to the playlist. The playlist order will be preserved on MySpace, so go ahead and order them if you want. All of the songs on the playlist will be transcoded to MP3 and uploaded to your server, so don’t choose more than four or five songs to save your own bandwidth, time, and disk space (I’d recommend choosing two.)

Warning Currently, iTunes will not transcode files from the iTunes Music Store to MP3, as doing so breaks the copy protection. All iTunes-protected songs will be skipped by the script, so stick to non-protected MP3 and AAC files for uploading to MySpace.

Once you’re satisfied with your selection, run the Export to MySpace script. The script will use iTunes to convert all of your files to MP3 and upload each one to your FTP server, one by one. When the script is done, it will then upload the XML-based playlist to your server and show you a message that the script is complete. If you never see the complete message, that means that you’re still uploading in the background or the script failed someplace. Note that if you’re uploading a lot of files, the script can take a very long time depending on your connection speed, so if you’re on aDSL like I am you can expect waits of approximately twenty minutes to upload four or five songs.

Configuring the XSPF player

Once you’ve got everything exported, you’ll need to point your music player at the new playlist. If you haven’t set up the XSPF player out of the hack pack, you’ll need to read this tutorial. (Don’t worry about the XML part of it, as that’s covered by the Export script.)

Your <embed> string should point to a file called generated_playlist.xml within the directory you have specified in the http_server variable above. For example, in our above file, the playlist_url parameter of the embed string should point to http://myserver.com/myspacemp3/generated_playlist.xml. If your player is located at http://myserver.com/player.swf, your embed string should look a lot like the one below:

<embed src="http://myserver.com/player.swf? playlist_url=http://myserver.com/myspacemp3/generated_playlist.xml">

If this is correct, the XSPF player will then be able to read the generated playlist and your MySpace profile page will need no other modifications to use the script.

Cleaning up the FTP disk space

There are a couple of things that the script doesn’t do, though, and I’m fairly sure it’s due to a limitation of my own AppleScript knowledge and not so much a limitation of AppleScript itself. One such thing is that the MP3s are left on the server even if they’re not on the playlist, so if you change your playlist frequently you are going to want to clean out the directory you are uploading MP3s to every other time or so you upload MP3s, otherwise disk space will be quickly consumed by MP3 files. To do this, just delete everything in the directory; the script will automatically reupload anything that’s not in the specified directory.

At this point, everything should work. Test your script and test your MySpace page; you should have a working playlist that mirrors the one you have in iTunes. If you want to see it in action elsewhere, try my MySpace page. How’s that for integration?