Erik A. Hanson's Weblog

AppleScripts for Creating Podcast Playlists in iTunes

Posted: March 16th, 2009    Tags: AppleScript, My Software

I have a 2nd generation iPod Shuffle that I use with the super-awesome Arriva iPod headphones:

arriva

I love these headphones because I can wear them at the gym without having to deal with wires. I actually bought the shuffle because of these headphones. At the gym, I like to start out by listening to a few news podcasts while I warm up and then I like to hear some random music.

Unfortunately, version 8.1 of Apple’s iTunes removed the ability to autofill podcasts on the shuffle. (See this thread on Apple’s support page for example.)

Luckily, it can still be done with AppleScript. Here’s a script that copies all of the songs from my “Gym” playlist, including podcasts, to my shuffle:

tell application "iTunes"
  repeat with thisSource in sources
    if the name of thisSource = "Erik's Shuffle" then set myIpod to thisSource
  end repeat

  set destinationPlaylist to the first playlist in myIpod
  delete tracks in destinationPlaylist

  set sourcePlaylist to playlist "Gym"
  if the number of tracks in sourcePlaylist is greater than 0 then
    set shufflable of (every track of sourcePlaylist) to true
    duplicate every track of sourcePlaylist to destinationPlaylist
  end if
end tell

And here’s the full script that I use these days. It deletes everything on the shuffle, then adds the contents of three of my podcast playlists (which are “smart playlists” that only contain the most recent unplayed episode), and then adds one song from a short list of songs I like to start my workout with, and then finally adds 25 random songs from a collection of music that’s suitable for the gym.

on addRandomSongs(sourcePlaylistName, destinationPlaylist, songCount)
  tell application "iTunes"
    set sourcePlaylist to playlist sourcePlaylistName
    set alreadyUsedTrackNumbers to {}
    set numberOfTracksInSourcePlaylist to the number of tracks in sourcePlaylist
    repeat songCount times
      set trackNumber to (random number from 1 to numberOfTracksInSourcePlaylist)
      if alreadyUsedTrackNumbers does not contain trackNumber then
        duplicate (track trackNumber of sourcePlaylist) to destinationPlaylist
        copy trackNumber to the end of alreadyUsedTrackNumbers
      end if
    end repeat
  end tell
end addRandomSongs

on addPodcast(sourcePodcastName, destinationPlaylist)
  tell application "iTunes"
    set sourcePodcast to playlist sourcePodcastName
    if the number of tracks in sourcePodcast is greater than 0 then
      set shufflable of (every track of sourcePodcast) to true
      duplicate every track of sourcePodcast to destinationPlaylist
    end if
  end tell
end addPodcast

on findDevice(deviceName)
  tell application "iTunes"
    repeat with thisSource in sources
      if the name of thisSource = deviceName then return thisSource
    end repeat
  end tell
end findDevice

--
-- begin
--

set myShuffle to findDevice("Erik's Shuffle")

-- delete all tracks from the shuffle
tell application "iTunes"
  set shufflePlaylist to the first playlist in myShuffle
  delete tracks in shufflePlaylist
end tell

-- add podcasts
addPodcast("NPR Hourly News Summary", shufflePlaylist)
addPodcast("California Report", shufflePlaylist)
addPodcast("NPR Story Of The Day", shufflePlaylist)

-- add songs
addRandomSongs("Gym Kickoff", shufflePlaylist, 1)
addRandomSongs("Gym Music", shufflePlaylist, 25)

To use either of these scripts, fire up ScriptEditor, paste the script in, choose “Save As” from the “File” menu and choose “Application” as the file format, and then save it in your Library/Scripts/Applications/iTunes folder. A script menu will appear in the right portion of your menu bar when you’re in iTunes.



4 Responses to “AppleScripts for Creating Podcast Playlists in iTunes”

  1. Paul Says:

    Hi I’m trying to follow your instructions but I can’t find this folder “Library/Scripts/Applications/iTunes folder”
    I’m using leopard…

  2. Paul Says:

    and when I try to run the script from the desktop I get “file permission error”. Thanks for any help as this sounds like exactly what I need..

  3. Paul Says:

    ok I think I got it to work from the desktop as long as itunes is closed even though it still gives me a file permission error, but it deleted all the files I had on my shuffle. How do I get it to just add the podcasts, but keep all the existing files on my shuffle?

  4. Erik Says:

    Hi Paul, if you don’t see a “Library/Scripts/Applications/iTunes” folder, you can create it. Make sure you put it the “Library” folder in your home directory, not the “Library” folder at the root of your hard disk.

    I’m not sure why you’d get a file permission error when running the script. If you run the script from within ScriptEditor, it might tell you which line of the script is causing the file permission error.

    As far as not deleting everything on your shuffle first: the first script on this page doesn’t delete everything, it just copies the tracks from one playlist to your shuffle. The second script does delete everything, but you can fix that by removing the 4 lines below the line “– delete all tracks form the shuffle”.

Leave a Reply



(Comments are moderated to keep out spam. Please be patient.)