iTunes has a convenient New Playlist From Selection command (available from the File menu) to create a new playlist from a bunch of selected tracks. The only drawback is that it creates the playlist in the current source. This AppleScript hack lets you select some tracks in iTunes and send them to your iPod, optionally placing them in a newly created playlist. It even suggests a default name for the new iPod playlist based on the info from the selected tracks.
Running the Hack
Enter the code into Script Editor . Save this script in your iTunes Scripts folder as an applet (set the File Format in the Save As… dialog to Application) and name it New iPod Playlist from Selection.
TIP
Remember that the continuation character (
) is used to segment single lines of code so they look better in print; you can omit them in your code if you like.
Make sure your iPod Options are set to "Manually manage tracks and playlists" (if the iPod is set to auto-sync, files might be prevented from being moved) and that your iPod is connected. Select some library tracks in iTunes, and run the script. You will be asked to create a name for the new iPod playlist to which the selected tracks will be copied; optionally, you can skip making the playlist and just leave it at adding the tracksto your iPod library.
Now, fill up that iPod with playlists, and prosper!
—Doug Adams
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.