Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annard/efd935dc0a3c30f10cb7 to your computer and use it in GitHub Desktop.
Save annard/efd935dc0a3c30f10cb7 to your computer and use it in GitHub Desktop.
"Generate random music playlist" for iTunes: allows you to do random classical albums based on the movements which is impossible using the iTunes shuffle play and smart scripts. A great way to fill up your iDevice with a decent collection of classical music!
(*
"Generate random classical music playlist" for iTunes
written by Annard Brouwer (10/05/2014)
based on:
"Play Random Album" for iTunes
originally written by Paul Withey
updated by Doug Adams
This program is free software released "as-is"; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
or visit http://www.gnu.org/copyleft/gpl.html
*)
-- if you like, you can change these:
property randomPlaylistName : "[-Some Random Classical Albums>"
property minimumTracksRequired : 3
property maxPlaylistSize : 10 * 1024 * 1024 * 1024
tell application "iTunes"
-- Note that I have a series of smart playlist that weed out my music in different classes.
-- You may need to modify this to suit your purposes.
set lookInPlaylist to some playlist whose name is "Classic Mix"
try
set myRandomPlaylist to user playlist randomPlaylistName
delete every track of myRandomPlaylist
on error
set myRandomPlaylist to make new playlist with properties {name:randomPlaylistName}
end try
repeat -- Fill up the play list
local playlistTracks
repeat -- Find suitable album of minimum number of tracks
try
local theAlbumTracks
set someTrack to some track of lookInPlaylist whose album is some item of my getAlbums()
set discCount to get disc count of someTrack
if discCount ≤ 1 or discCount is missing value then
set theAlbumTracks to tracks whose album is (get album of someTrack)
else
set theAlbumTracks to {}
repeat with discNumber from 1 to discCount
-- This will make sure to get all tracks even spread over multiple discs
set discTracks to (tracks whose album is (get album of someTrack) and disc number is discNumber)
set theAlbumTracks to theAlbumTracks & discTracks
end repeat
end if
if length of theAlbumTracks ≥ minimumTracksRequired then
set playlistTracks to theAlbumTracks
exit repeat
end if
end try
end repeat
repeat with thisTrack in playlistTracks
try
(my copyTrack:thisTrack toPlaylist:myRandomPlaylist)
end try
end repeat
-- Make sure not to make it too big
if the size of myRandomPlaylist > maxPlaylistSize then exit repeat
end repeat
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment