Hack 27 Sharing Your Listening Preferences


iTunes and a little hackery make sharing your currently playing track a snap.

Listening to music while you work is a given for a large portion of the computing populace. Without music, the halls and cubicles seem a little lonelier, the days seem a little slower, and eating is more lifeless than usual. Music is a part of our computing lives, and being without it can be traumatizing.

Just as we enjoy listening, we also lust after more music to whet our auditory ensembles. Hearing an unknown artist, falling in love, and then orchestrating a search for MP3s or used CDs is a part of many of our lives as well. Suggestions from friends is a crucial part of our discovery process.

With iTunes, telling people what you listen to is easier than you think. Utilities like the donationware Kung-Tunes (http://www.kung-foo.tv/itti.php) publish information on your currently playing iTunes track on your web site, allowing the world to tune in to your musical enjoyment. Alternatively, use the freeware Moa Tunes (http://beam.to/woodenbrain) to alter the signature of your Entourage or Eudora email messages with the music that accompanied your response. Even Inter-Relay Chat (IRC) programs are getting into the act, as the shareware Snak (http://www.snak.com/) provides a MusicalOSX script that will display tracks in IRC channels.

If that's not enough, and you want iTunes integration with every instrument you use daily, check out the following piece of AppleScript, which will put the currently playing song, artist, and album into the clipboard. Simply run, paste, and seduce!

tell application "iTunes"
  set iname to name of current track
  set iartist to artist of current track
  set ialbum to album of current track
end tell

tell application "Finder"
  set music to "\"" & iname & "\" by " & iartist & " on the album \"" RETURN
  & ialbum & "\"."
  set the clipboard to the music
end tell

Figure 3-48 shows the result of running this script.

Figure 3-48. Passing on your listening preferences

If you're not an AppleScript roadie, you could use a bit of Perl to ftp the last 10 songs you've heard to your web site. The following code uses the MacOSX::iTunes (http://sourceforge.net/projects/brian-d-foy) module:

#!/usr/bin/perl -w
use strict;
use Mac::iTunes;
use Net::FTP;

# rather self-explanatory settings
my $itunes_library = "/Users/morbus/Music/iTunes/iTunes 3 Music Library";
my $itunes_playlist = "Recently Played";
my $ftp_host = "ftp.disobey.com";
my $ftp_username = "anonymous";
my $ftp_password = "morbus\@disobey.com";
my $ftp_path = "/incoming";
my $ftp_file = "itunes.html";

# data collector
open(FILE, ">$ftp_file") or die "couldn't create temp file: $!";
print FILE "<html><title>iTunes, $itunes_playlist</title>";
print FILE "<body><h1>iTunes, $itunes_playlist</h1><ul>";

# our library object
my $itunes = MacOSX::iTunes->read ( $itunes_library );
die "could not load the iTunes library" unless ref $itunes;

# and our playlist. We loop through tracks, adding to output.
my $playlist = $itunes->get_playlist( $itunes_playlist );
foreach my $track ( $playlist->items ) {
  print FILE "<li>", $track->as_string, "</li>";
print FILE '</ul></body></html>'; close FILE;

# and now send to our FTP site.
my $conn = Net::FTP->new($ftp_host) or die "could not connect to host: $!";
$ftp->login($ftp_username, $ftp_password);
$ftp->mkdir($ftp_path, 1); 
$ftp->put($ftp_file) or die "could not upload file: $!";

iTunes has struck a chord with many music lovers, and these hacks can help others get in on your groove. (Yes, we know. There were far too many musical innuendos in this hack for it to be considered good taste. Blame the composer, not the maestro.)