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" 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
try
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
Initially, I set some variables to be used later in the script. A choose from liststatement displays a list of the user playlists stored in the variable allUPNames.The result is a list of the names of the selected playlists, which will be stored in the variable selPlaylists.
Then, the user is asked for a name for the new playlist that will be created. The default name I initialized at the beginning of the script as defPNamewill be displayed, but the user can change this and enter something else in the text box, as shown in .
Figure 1. Entering a name for the playlist that will be created
The script checks to see if the name entered is already the name of a playlist and, if so, gives the user the option to replace the original playlist or go through the repeat loop and enter another new name. Then, the new playlist is created and a repeat block loops through the selected playlists and copies the tracks from each to the new playlist. Finally, the new playlist is shuffled (you can increase the repeat variable to get a more rigorous shuffle result) and selected in the source list, and it starts playing.