- Code: Select all
property randomAlbumName : "[-Some Random Album>"
property minimumTracksRequired : 5
set spareTracks to {}
tell application "iTunes"
repeat
try
set someTrack to some track of (some playlist whose special kind is music) whose album is some item of my getAlbums()
set theAlbumTracks to tracks whose album is (get album of someTrack) and disc number is (get disc number of someTrack)
if length of theAlbumTracks ≥ minimumTracksRequired then exit repeat
end try
end repeat
try
set myRandomPlaylist to user playlist randomAlbumName
delete every track of myRandomPlaylist
on error
set myRandomPlaylist to make new playlist with properties {name:randomAlbumName} -- , shuffle:true}
end try
repeat with n from 1 to length of theAlbumTracks
set chk to false
repeat with thisTrack in theAlbumTracks
try
if track number of thisTrack = n then
set chk to true
(my copyTrack:thisTrack toPlaylist:myRandomPlaylist)
exit repeat
end if
end try
end repeat
if (chk is false) then set end of spareTracks to thisTrack
end repeat
if spareTracks is not {} then
repeat with thisTrack in spareTracks
(my copyTrack:thisTrack toPlaylist:myRandomPlaylist)
end repeat
end if
play myRandomPlaylist
try
reveal myRandomPlaylist
end try
activate
end tell
to copyTrack:aTrack toPlaylist:thePlaylist
tell application "iTunes"
try
if not (exists (some track of thePlaylist whose database ID is (get aTrack's database ID))) then
duplicate aTrack to thePlaylist
end if
on error m
log m
end try
end tell
end copyTrack:toPlaylist:
to getAlbums()
set theCommand to "perl -e 'local $/=undef;my $s=<>;my @al=();my %seen=();while ($s=~m:<key>Album</key><string>(.*?)</string>:sg){$g = $1;$g=~ s:\\&\\#(\\d*);:chr($1):ge;$g=($g.\"" & (ASCII character 198) & "\");push(@al,$g)unless $seen{$g}++;}print @al;
' " & theDatabase()
set g to textToList((do shell script theCommand), (ASCII character 198))
if last item of g is "" then set g to items 1 thru -2 of g
return g
end getAlbums
on theDatabase()
try
set x to (do shell script "perl -e'open(T,\"defaults read com.apple.iApps iTunesRecentDatabasePaths|\");while(<T>){$t.= $_;}close(T);$t=~m|\"(.*?)\"|s;$t=$1;$t=~s| |\\\\ |g;print $t;'")
return quoted form of (do shell script "ls " & replaceChars(escapeForUnix(x), "'", "\\'"))
on error m
log m
end try
end theDatabase
on replaceChars(txt, srch, repl)
set text item delimiters to the srch
set the item_list to every text item of txt
set text item delimiters to the repl
set txt to the item_list as string
set text item delimiters to ""
return txt
end replaceChars
on textToList(txt, delim)
set saveD to text item delimiters
try
set text item delimiters to {delim}
set theList to every text item of txt
on error errStr number errNum
set text item delimiters to saveD
error errStr number errNum
end try
set text item delimiters to saveD
return (theList)
end textToList
to escapeForUnix(n)
set badlist to "!@#$%^&*()+=-{}[]:;?<>"
set filled to ""
repeat with i from 1 to (length of n)
set t to (text i of n)
if t is in badlist then
set filled to (filled & "\\" & t)
else
set filled to (filled & t)
end if
end repeat
return filled
end escapeForUnix