I’ve written a script to choose Albums at random and throw them onto a playlist called “Pack the 'Pod” and have a few problems. The first problem is speed. Because I don’t want any duplicate albums in the playlist, I’m having the script maintain a list of all the albums it has encountered and checking any new albums against that list to make sure no duplicates show up. Naturally, this becomes a very slow proccess as the list builds. I’m wondering if there is a better way to approach this problem as it currently takes about 6 minutes to fill the playlist to 15 gigs.
My second problem is the matter of checking to see if the playlist exists on the offchance that it’s a first run. This is a relatively smaller problem as it introduces new problems. In other words, if I set the script to delete the playlist and create a new one each time, the iPod looses the preference indicating it needs to be updated, and it’s passed by, so fixing the problem seems to raise more headaches than is worth to me, but if you know the answer, I’d love to hear.
Last, but not least, is the dashed out couple of lines. Those are there because I don’t want tracks that are deselected in the main window to be copied, but if you can figure out how to make those lines work, too, that’d be groovy!
Finally, here’s the code!
tell application "iTunes"
set album_list to {}
delete every track of playlist "Pack the 'Pod"
repeat with i from 1 to the count of sources
if the kind of source i is iPod then
set thePod to the name of source i as text
end if
end repeat
set theCapacity to the text returned of (display dialog "How full shall I pack'r, Sir? (Please give your answer in Gigs)" default answer 15 giving up after 300)
set theCapacity to theCapacity * 1.0E+9 as real
set theSize to (size of playlist "Pack the 'Pod") as real
set randomLibrary to playlist "Library" of source "Library"
set shuffle of randomLibrary to true
set shuffle of randomLibrary to false
set shuffle of randomLibrary to true
set trackCounter to 1
repeat while theSize < theCapacity
-- if enabled of trackCounter of randomLibrary is true then
set albumName to album of track trackCounter of randomLibrary
if albumName is "" or album_list contains albumName then
set trackCounter to trackCounter + 1
else if album_list does not contain albumName then
try
copy (get a reference to (every track in randomLibrary whose album is albumName)) to albumLister
duplicate albumLister to playlist "Pack the 'Pod"
on error
-- keep going?
end try
set trackCounter to trackCounter + 1
set theSize to size of playlist "Pack the 'Pod" as real
set album_list to (album_list & albumName) as list
end if
-- end if
end repeat
update thePod
end tell
fs
p.s. Jonn, did I do something wrong in the post? The link to ‘Open this script in a new window’ doesn’t seem to work.