Build a Random Playlist

Shuffle several playlists together into a single random playlist.

Here’s a hack a Smart Playlist or even Party Shuffle can’t pull off: pick a bunch of playlists, combine all their songs into a single new playlist, shuffle ‘em up, and play.

The Code

This script allows the user to select two or more playlists and then copy the tracks into a new user-named playlist. The new playlist will then be shuffled and played.

    tell application "iTunes"

      -- variables used in choose from list
      set my_prompt to "Select playlists…"
      set my_ok_button to "Select…"
      set my_cancel_button to "Quit"

      -- default name of new playlist
      set defPName to "Random Bunch"

      -- list of all playlist names
      set allUPNames to name of user playlists

      -- select the playlist names to combine
      set selPlaylists to (choose from list allUPNames with prompt my_prompt ¬
        OK button name my_ok_button cancel button name my_cancel_button ¬
        with multiple selections allowed)

      if selPlaylists is false then error number -128

      -- get name for new playlist
      repeat
        set newPName to text returned of ¬
          (display dialog "Enter a new Playlist name:" default answer defPName ¬
            buttons {"Cancel", "OK"} default button 2)

        -- handle existing playlist
        if allUPNames does not contain newPName then
          exit repeat
        else if button returned of (display dialog (newPName & ¬
          " exists. Replace?") buttons {"No", "Yes"} ¬ default button 1 with icon 2) is "Yes" ...

Get iPod and iTunes Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.