-- iTunes Group Randomizer -- This script operates on the selected playlist in iTunes. It randomizes the tracks, preserving the ordering within contiguous tracks with the same album and grouping. This is very useful in randomizing classical works. Just make sure that the unit you want preserved has the same grouping and the same album. -- Note: The script assumes that the initial playlist is sorted by album and track #. -- Created by Rob Lopez, 2004. Feel free to use as you please. tell application "iTunes" activate beep -- initialize list variables set newList to {} -- list holding remaining items in playlist to be randomized set tempList to {} -- temporary list copied to newList at every iteration set beginningList to {} -- initial tracks sans deleted tracks set endingList to {} -- final tracks sans deleted tracks -- first, grab the current playlist set thePlaylist to (get view of front window) -- determine the playlist name for later renaming the randomized playlist set namePlaylist to name of thePlaylist set ranPlaylist to (make new user playlist) set name of ranPlaylist to "tempRandom" -- Sort the playlist initially: how do we do this in AppleScript? -- For now, assume that the user has sorted so that groups are contiguous -- trackCount = the length of the initial playlist set initialTrackCount to (get index of last track of thePlaylist) set trackCount to initialTrackCount -- create a list of all files in the playlist repeat with i from 1 to trackCount set newList to newList & (get location of track i of thePlaylist) end repeat -- Randomizer loop repeat while trackCount is greater than 0 -- grab a random track set currentItem to (random number from 1 to trackCount) set theTrack to currentItem -- get the album and group for the random track set testTrack to (get track theTrack of thePlaylist) set testAlbum to (get album of testTrack) set testGroup to (get grouping of testTrack) -- display dialog "currentItem = " & currentItem & ", album = " & testAlbum & " group = " & testGroup -- find the beginning of the group set beginningTrack to 1 repeat while theTrack is greater than 1 -- loop up to find the beginning set theTrack to (theTrack - 1) if ((album of track theTrack of thePlaylist) is not equal to testAlbum) or ((grouping of track theTrack of thePlaylist) is not equal to testGroup) then set beginningTrack to theTrack + 1 exit repeat end if end repeat -- display dialog "Beginning track = " & beginningTrack -- find the end of the group set theTrack to currentItem set endTrack to trackCount repeat while theTrack is less than trackCount -- loop down to find the end set theTrack to (theTrack + 1) if ((album of track theTrack of thePlaylist) is not equal to testAlbum) or ((grouping of track theTrack of thePlaylist) is not equal to testGroup) then set endTrack to theTrack - 1 exit repeat end if end repeat -- display dialog "End track = " & endTrack -- grab the tracks from the beginning to the end, add to new playlist, and delete from old playlist repeat with i from beginningTrack to endTrack add item i of newList to ranPlaylist delete file track beginningTrack of thePlaylist -- display dialog "Deleted track " & beginningTrack & " from playlist" end repeat -- if the extracted work was not at the beginning, create the beginning list if beginningTrack > 1 then set beginningList to items 1 thru (beginningTrack - 1) of newList else set beginningList to {} end if -- if the extracted work was not at the end, create the ending list if endTrack < (count newList) then set endingList to items (endTrack + 1) thru (count newList) of newList else set endingList to {} end if -- create the list of tracks left after extraction set tempList to beginningList & endingList set newList to tempList -- trackCount = tracks left to randomize set trackCount to count newList end repeat -- copy the randomized playlist to the original list delete thePlaylist set name of ranPlaylist to namePlaylist beep display dialog "The playlist " & (get name of ranPlaylist as string) & " with " & initialTrackCount & " tracks was randomized." end tell