O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
iPod and iTunes Hacks
By Hadley Stern
October 2004
More Info

HACK
#45
Feed Streaming Audio to Your iPod
Capture an Internet audio stream to an MP3 file, save the file to your hard drive, and automatically upload those files to your iPod when it connects to your computer.
The Code
[Discuss (0) | Link to this hack]

The Code

I really have two problems: I need a script that does what I want it to do and something needs to see the iPod is attached to my computer and run the script.

Choosing which language can be pretty important, especially when you're trying to connect multiple programs. Most of Apple's programs can work with shell scripting or Perl, and I prefer their speed and flexibility to other scripting languages. But Apple has built AppleScript into iTunes, the program I want, so it's a little easier to work with AppleScript for this task.

Here's the AppleScript I use to copy my files to my iPod:

property thePlaylist : "TestPlaylist"
property theiPod : "Professor Processor"
property theRecordingFolder : "Powerbook 
    G4:Users:stevko:Desktop:hijack"
property theStorageFolder : "Powerbook 
    G4:Users:stevko:Desktop:hijack:storage"
property tempPlaylist : "TempPlaylist"
property timeToKeepFilesInDays : 7

set fileList to (list folder theRecordingFolder)


tell application "iTunes"

   set onList to {}
   try
       set pod to source theiPod
   on error
       display dialog 
           "Could not find iPod " & theiPod buttons {"Cancel"}
   end try
   try
       set podPl to user playlist thePlaylist of pod
   on error
       display dialog 
           "Could not find playlist " & thePlaylist & " on iPod " &
theiPod buttons {"Cancel"}
   end try

   if fileList is not {} then

      -- create temp playlist
      try
         set tempPl to (make new user playlist)
         set name of tempPl to tempPlaylist
      on error
         display dialog 
             "Could not create playlist with name " 
                 & tempPlaylist buttons {"Cancel"}
      end try

      -- add files to temp playlist

      repeat with q from 1 to (count of fileList)
          set thisFile to item q of fileList
          if thisFile is not ".DS_Store" then
              try
                  set pth to theRecordingFolder & ":" & thisFile
                  add file pth to tempPl
              on error
                  -- ignore; if we can't get one,
                  -- we might be able to get more
              end try
          end if
      end repeat

      -- find if any of the names of the temp playlist items
      -- are the same as the ones on the iPod playlist

      set deleteList to {}
      repeat with r from 1 to (count of tracks in tempPl)
          set temptrack to track r of tempPl

          repeat with s from 1 to (count of tracks in podPl)

              set podtrack to track s of podPl
              if name of podtrack is name of temptrack then
                  set n to name of temptrack
                  set deleteList to deleteList & n
                  exit repeat
              end if
          end repeat
      end repeat

      repeat with y in deleteList
          try
              delete track y in tempPl
          on error
              -- again, ignore; it's nice to delete extras,
              -- but not necessary
          end try
      end repeat

      -- remove any from iPod that are more than a week old,
      -- or that have already been heard.
      -- THIS DOES DELETE FILES OFF OF YOUR IPOD WITHOUT A WARNING!!

      -- (That's one of the points of this Applescript)

      set delList2 to {}
      repeat with p from 1 to (count of tracks in podPl)
          set ptt to track p of podPl
          if (played count of ptt is greater than 0) or 
              (played date of ptt is ((current date) 
                  -(timeToKeepFilesInDays * days))) then
             set pptn to name of ptt
             set delList2 to delList2 & name of ptt
          end if
      end repeat
      repeat with y in delList2
          try
              delete track y in podPl
          on error
              -- again, ignore
          end try
      end repeat

      -- copy everything that's on playlist to the iPod
      -- optionally, this will also convert the files
      -- at the same time; uncomment the line below to do so.
      repeat with m from 1 to (count of tracks in tempPl)
          set mt to track m of tempPl
          -- convert mt
          duplicate mt to podPl
      end repeat

      -- remove temp playlist

      set delList3 to every track in tempPl
      repeat with t in delList3
          delete track (name of t) in library playlist 1 
              in source "Library"
      end repeat
      delete tempPl

  else
      display dialog 
          "Could not find any files in folder " 
              & theRecordingFolder buttons {"Cancel"}
  end if
end tell

-- this moves the files from the recording folder to a storage folder-- if you use delete instead of move, you can trash the files instead.
tell application "Finder"
    repeat with t in fileList
        try
            move file t in folder theRecordingFolder to
theStorageFolder
        on error
            display dialog 
                "Could not move file" & name of t & " from " 
                    & the Recording Folder & " to " 
                    & the Storage Folder buttons {"Cancel"}
        end try
    end repeat
end tell

This AppleScript has five major functions. The first is to find the iPod and the playlist on the iPod that we're going to use. If we can't find these, it errors out automatically. After all, there's no point in uploading to an iPod's playlist if you can't get to the iPod or the playlist.

Second, it creates a temporary playlist of all of the items inside our Audio Hijack recording folder and then sees if any of these new files are already on the iPod (no point in reloading files that are already there). The temporary playlist is necessary because we need to compare apples and apples, not apples and oranges—in this case, files and tracks. The files inside of the Hijack folder are just that, files, while the files on our iPod register only as tracks in iTunes. The name of a file is its filename, and the name of a track is the ID3 tag name; and those often don't match. The easiest way to compare the two sets of files is to make them the same type, and doing this through iTunes is a tad easier, especially since we want to build a playlist with these files anyway.

After determining which files aren't to be found on the iPod, the script then moves on to determine which files are on the iPod and need to be removed. The criteria for files to be removed includes items that have been played through once or files that are more than n days old. In this case, n is the property timeToKeepFilesInDays; set it to suit your fancy. Once a file meets either of those criteria, the file is trashed; in theory, we don't want it anymore. The script also trashes these files because getting files off of an iPod is another hack altogether, and I want to stick to the topic.

After removing the unneeded iPod files, the script copies the files on the temporary playlist to the iPod. The temporary playlist should contain only the subset of files that were in the folder and were not already on the iPod when the script started. So, you should get a nice batch of new files on your machine and get rid of the old ones at the same time so that you don't fill up the iPod with old tracks.

Finally, the script performs a little cleanup. First, it gets rid of the tracks we added temporarily, by deleting them from the Library. When you add tracks, they automatically go into the library. Since you don't want them added permanently, kill them off. The script then kills off the temporary playlist, just to make things nice again. Finally, it moves the files from the recording folder to a storage folder in case it's needed later.

So, now you have an AppleScript that copies what you want to the iPod. To finish, we need something to kick off the AppleScript when the iPod connects. As mentioned, there are several applications that do just that. I like iPod Scripter the best, because it's only a System Preferences panel installation. iPod Launcher, on the other hand, is a background application and preference panel that checks every specified number of minutes for the iPod. Both work equally well, and either will do.

Download either app and install it. The interfaces for the programs are similar: iPodScripter displays a list for applications and a second list for scripts (as shown in ), while iPod Launcher displays just one list for both. Just drag and drop the script on the appropriate list. The script runs whenever you dock your iPod.

Now that you're all set up, it's time to find some interesting shows, record them, and connect your iPod for a quick transfer. Enjoy the unlikely mix of sunshine and British broadcasting!

Ted Stevko

Figure 1. The iPodScripter user interface


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.