• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

5. AppleScript for iTunes > 82. Build a Random Playlist

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
        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" then
          delete user playlist newPName
          exit repeat
        end if

      end repeat

      -- create the new empty playlist
      set newPlaylist to ¬
        (make new playlist with properties {name:newPName})

      -- copy tracks from selected playlists to new playlist
      repeat with thisPlaylist in selPlaylists
          duplicate file tracks of playlist thisPlaylist to newPlaylist
        end try
      end repeat

      -- shuffle vigorously
      repeat 3 times
        set shuffle of newPlaylist to false
        set shuffle of newPlaylist to true
      end repeat

      -- select and play the new playlist
      set view of front browser window to newPlaylist
      play first track of newPlaylist
    end tell



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint