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
#83
Make a New iPod Playlist from Your Selections
Send selected tracks in iTunes straight to your iPod, and optionally create a new playlist to put them in.
The Code
[Discuss (0) | Link to this hack]

The Code

This script gets the locationproperty of each selected iTunes track and uses it as the target of a duplicatecommand, copying the file to a mounted iPod. Additionally, a new iPod playlist can be created (named using the artist and album of the first selected iTunes track) to which the newly added iPod tracks can be copied.

    tell application "iTunes"

      -- is iPod mounted?
      try
        set iPodSource to some source whose kind is iPod
      on error
        display dialog "Unable to detect iPod." buttons {"Cancel"} 
          default button 1 with icon 2 giving up after 15
        return
      end try

      -- selected tracks must be from library
      if selection is not {} and 
        (kind of container of view of front window) is library then

        set sel to get a reference to selection

        -- create a default name for new iPod playlist
        set suggestedPName to ""
        try
          tell item 1 of sel to set suggestedPName to 
            (((get artist) as string) & " - " & ((get album) as string))
        end try

        set myOptions to (display dialog 
          "Enter a name for the new iPod Playlist:" default answer 
          suggestedPName buttons {"Cancel", "No Playlist", "OK"} 
          default button 3)

        set new_iPod_tracks to {}
        repeat with thisTrack in sel

           -- if the track is able to be copied, copy it to iPod
           -- and add the reference to list of tracks
          try
            set end of new_iPod_tracks to 
              (duplicate thisTrack to library playlist 1 of iPodSource)
          end try
        end repeat

-- if user wants new playlist, create it and
-- copy tracks to it now
if button returned of myOptions is "OK" then
  set new_Pnom to text returned of myOptions

  set new_iPod_playlist to 
    (make new user playlist at iPodSource 
	  with properties {name:new_Pnom})

  repeat with thisTrack in new_iPod_tracks
    duplicate thisTrack to new_iPod_playlist
  end repeat

end if

display dialog "Done." buttons {"Thanks"} default button 1 
  with icon 1 giving up after 15

else
display dialog "Select some library tracks." buttons {"Cancel"} 
  default button 1 with icon 2 giving up after 15
end if 
end tell

The first thing the script does is make sure there's an iPod mounted and available to iTunes. If so, the iPodSourcevariable is set to its source.

Then the script determines if there are any selected tracks and if they are from a library playlist, rather than a CD playlist or radio tuner playlist (whose tracks can't be copied to an iPod). If the tracks pass muster, the sel variable is set to a list of references to the selected tracks. If they don't pass muster, the if block concludes with an else clause at the end of the script instructing the user to select some library tracks. When this dialog is dismissed, the script quits in order that the user can reselect the correct kind of tracks. The script must then be run again.

The script will use the artistand albumof the first track among the selected tracks to create a default name for the potential new iPod playlist. It's fast and convenient to set the default here, even though in the following routine the user may decide not to create a new playlist.

The display dialog asks for a new name for the playlist using the default name we just created (stored in suggestedPName), as shown in . However, the user can also decide not to copy the tracks to a new playlist by selecting the No Playlist button.

No matter what, the selected tracks will be copied to the iPod; copying them to a new iPod playlist is optional.

Figure 1. Entering a name for the new iPod playlist or deciding not to create a new playlist

The display dialog's result is a list that contains the text and button returned from the dialog. Those results are assigned to the myOptionsvariable, the items of which we will be using shortly.

Next is the routine that copies each track to the iPod. This is the last check for the correct kind of track. It will catch tracks whose files have gone missing (a.k.a. dead tracks), which can't be conveniently detected until this point in the script. It will also prevent errors from interrupting the script if a track, for some reason (if, for example, you have a few Ogg Vorbis files lying around your library), can't be copied to the iPod (the routine will skip over such tracks). Additionally, it creates a list of references to the new iPod tracks in the variable new_iPod_tracks.

If the button returned in myOptionsis OK, the script creates a new playlist on the iPod and copies a reference to it to new_iPod_playlist,then uses the text returned from myOptionsas its name. Finally, the repeat loop copies each track referenced in new_iPod_tracksfrom the iPod library to the new iPod playlist.


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.