This script can be selected as the AutoDJ source and it will fill the Now Playing list as specified in your options. When switched on, whenever you play the last track in your now playing list (N.B. turn shuffle off!) a random album will appear (in track order) at the end of your list. This means you will never run out of albums to listen to.

As always, the installation package is available to download from my website. Here's the code too...

Sub Install() 'set default values Dim ini : Set ini = SDB.IniFile ini.StringValue("AutoAlbumDJ","Enabled") = "0" 'switch off old version If ini.StringValue("AutoAlbumDJ","MinTracks") = "" Then ini.IntValue("AutoAlbumDJ","MinTracks") = MinTracks End If If ini.StringValue("AutoAlbumDJ","AvgRating") = "" Then ini.IntValue("AutoAlbumDJ","AvgRating") = AvgRating End If If ini.StringValue("AutoAlbumDJ","MinRating") = "" Then ini.IntValue("AutoAlbumDJ","MinRating") = MinRating End If If ini.StringValue("AutoAlbumDJ","Capacity") = "" Then ini.IntValue("AutoAlbumDJ","Capacity") = Capacity End If If ini.StringValue("AutoAlbumDJ","Attempts") = "" Then ini.IntValue("AutoAlbumDJ","Attempts") = Attempts End If If ini.StringValue("AutoAlbumDJ","TheGenre") = "" Then ini.StringValue("AutoAlbumDJ","TheGenre") = TheGenre End If If ini.StringValue("AutoAlbumDJ","Mode3f1") = "" Then ini.BoolValue("AutoAlbumDJ","Mode3f1") = Mode3f1 End If If ini.StringValue("AutoAlbumDJ","MaxTracks") = "" Then ini.IntValue("AutoAlbumDJ","MaxTracks") = MaxTracks End If If ini.StringValue("AutoAlbumDJ","Unplayed") = "" Then ini.BoolValue("AutoAlbumDJ","Unplayed") = Unplayed End If If ini.StringValue("AutoAlbumDJ","Earliest") = "" Then ini.StringValue("AutoAlbumDJ","Earliest") = Earliest End If If ini.StringValue("AutoAlbumDJ","Debug") = "" Then ini.BoolValue("AutoAlbumDJ","Debug") = Debug End If If ini.StringValue("AutoAlbumDJ","MaxDays") = "" Then ini.IntValue("AutoAlbumDJ","MaxDays") = MaxDays End If If ini.StringValue("AutoAlbumDJ","AvgPlays") = "" Then ini.IntValue("AutoAlbumDJ","AvgPlays") = AvgPlays End If If ini.StringValue("AutoAlbumDJ","IgnoreNon") = "" Then ini.BoolValue("AutoAlbumDJ","IgnoreNon") = IgnoreNon End If If ini.StringValue("AutoAlbumDJ","ModeNum") = "" Then ini.IntValue("AutoAlbumDJ","ModeNum") = ModeNum End If If ini.StringValue("AutoAlbumDJ","Toolbar") = "" Then ini.BoolValue("AutoAlbumDJ","Toolbar") = Toolbar End If If ini.StringValue("AutoAlbumDJ","FilterName") = "" Then ini.StringValue("AutoAlbumDJ","FilterName") = FilterName End If If ini.StringValue("AutoAlbumDJ","MinDays") = "" Then ini.IntValue("AutoAlbumDJ","MinDays") = MinDays End If If ini.StringValue("AutoAlbumDJ","Trayicon") = "" Then ini.BoolValue("AutoAlbumDJ","Trayicon") = Trayicon End If If ini.StringValue("AutoAlbumDJ","ApplyFilter") = "" Then ini.BoolValue("AutoAlbumDJ","ApplyFilter") = ApplyFilter End If

'add toolbar button Dim but : Set but = SDB.Objects("AADJ-Button") If but Is Nothing Then Set but = SDB.UI.AddMenuItem(SDB.UI.Menu_TbStandard,0,0) but.Caption = "Play a new album" but.IconIndex = SDB.RegisterIcon("Scripts\AADJ.ico",0) but.UseScript = SDB.ApplicationPath&"Scripts\AutoAlbumDJ3.vbs" but.OnClickFunc = "SelectAlbum" but.Visible = SDB.IniFile.BoolValue("AutoAlbumDJ","Toolbar") Set SDB.Objects("AADJ-Button") = but End If

'show form Set SDB.Objects("AADJOptsForm") = Form If Form.ShowModal = 1 Then Set edt = SDB.Objects("AADJModeCapt") If Not (edt Is Nothing) Then ModeNum = Form.Common.ChildControl("AADJModeNum").Value edt.Caption = "Enable "&ModeNum&"-from-1 mode (adds "&ModeNum&" random tracks from selected album)" End If End IfEnd Sub

If SaveConfig Then ini.BoolValue("AutoAlbumDJ","Mode3f1") = Sheet.Common.ChildControl("AADJMode3f1").Checked ini.BoolValue("AutoAlbumDJ","Debug") = Sheet.Common.ChildControl("AADJDebug").Checked If ini.BoolValue("AutoAlbumDJ","Debug") Then clr = True End If

mem = Form.Common.ChildControl("AADJEarliest").Text If Not (mem = "") Then If ValidDate(mem) Then ini.StringValue("AutoAlbumDJ","Earliest") = mem Else ini.StringValue("AutoAlbumDJ","Earliest") = "" Call SDB.MessageBox("AutoAlbumDJ: Earliest date invalid, should be YYYYMMDD.",mtError,Array(mbOk)) End If End If End If End If

Set SDB.Objects("AADJClearBtn") = Nothing Set SDB.Objects("AADJClearBtn2") = Nothing Set SDB.Objects("AADJModeCapt") = Nothing If clr Then Call clear() End IfEnd Sub

Sub DoSelectAlbum() Call SelectAlbum(Nothing)End Sub

Sub SelectAlbum(but) Debug = SDB.IniFile.BoolValue("AutoAlbumDJ","Debug") If Debug Then Call out("[SelectAlbum]") End If

Dim song : Set song = SDB.Player.CurrentSong If song Is Nothing Then Exit Sub End If Dim aid : aid = song.Album.ID If aid < 0 Then Exit Sub End If

Dim list : Set list = SDB.Objects("AADJSongList") If Not (list Is Nothing) Then If list.Count > 0 Then If list.Item(0).Album.ID = aid Then Set SDB.Objects("AADJSongList") = Nothing SDB.IniFile.StringValue("AutoAlbumDJ","SongList") = "" End If End If End If

Function GenerateNewTrack Debug = SDB.IniFile.BoolValue("AutoAlbumDJ","Debug") If Debug Then Call out("[GenerateNewTrack]") End If

Dim iter,i,str,arr Dim list : Set list = SDB.Objects("AADJSongList") If Not (list Is Nothing) Then If list.Count = 0 Then Set list = Nothing End If End If If list Is Nothing Then str = SDB.IniFile.StringValue("AutoAlbumDJ","SongList") If str = "" Then Call AutoAlbumDJ(list) Else arr = Split(str,",") Set list = SDB.NewSongList For i = 0 To UBound(arr) Set iter = SDB.Database.QuerySongs("ID="&arr(i)) If Not iter.EOF Then Call list.Add(iter.Item) End If Next If list.Count = 0 Then Set list = Nothing End If End If End If

If list Is Nothing Then FilterName = SDB.IniFile.StringValue("AutoAlbumDJ","FilterName") FilterSQL = GetFilterSQL(FilterName) Set iter = SDB.Database.OpenSQL("SELECT ID FROM Songs WHERE ID>0 "&FilterSQL&" ORDER BY Random(*) LIMIT 1") Set iter = SDB.Database.QuerySongs("ID="&iter.ValueByIndex(0)) Set GenerateNewTrack = iter.Item If Debug Then Call out("Random track: "&GenerateNewTrack.Title&" ("&GenerateNewTrack.ID&")") End If Set SDB.Objects("AADJSongList") = Nothing Else Set GenerateNewTrack = list.Item(0) If Debug Then Call out("Next track: "&GenerateNewTrack.Title&" ("&GenerateNewTrack.ID&")") End If If list.Count = 1 Then If Debug Then Call out("(End of list)") End If Call AutoAlbumDJ(list) Else Call list.Delete(0) End If Set SDB.Objects("AADJSongList") = list End If

str = "" If Not (list Is Nothing) Then If list.Count > 0 Then str = list.Item(0).ID For i = 1 To list.Count-1 str = str&","&list.Item(i).ID Next End If End If SDB.IniFile.StringValue("AutoAlbumDJ","SongList") = strEnd Function

'loop until valid album found Do boo = True ind = Int((max-min+1)*Rnd)+min 'ensure album name isn't blank If IgnoreNon Then Set iter = SDB.Database.OpenSQL("SELECT Album FROM Albums WHERE ID="&ind) If iter.EOF Then boo = False If Debug Then Call out(ind&" not in database") End if Else If iter.StringByName("Album") = "" Then boo = False If Debug Then Call out(ind&" has no name") End if End If End If End If 'ensure album hasn't been selected recently If (InStr(mem,"|"&ind&"|") > 0) Or (boo = False) Then If boo Then boo = False If Debug Then Call out(ind&" is in memory") End If End If Else 'ensure there are enough tracks with minimum rating and matching genre and within filter If TheGenre = "" Then str = "SELECT Count(*) As Nombre FROM Songs WHERE IDAlbum="&ind&" AND Rating>"&minrat&" "&FilterSQL Else str = "SELECT Count(*) As Nombre FROM Songs,GenresSongs,Genres WHERE Songs.IDAlbum="&ind&" AND Songs.Rating>"&minrat&" AND GenresSongs.IDSong = Songs.ID AND GenresSongs.IDGenre = Genres.IDGenre AND Genres.GenreName IN ('"&Replace(TheGenre,",","','")&"') "&FilterSQL End If Set iter = SDB.Database.OpenSQL(str) tot = Int(iter.ValueByName("Nombre")) If (tot*1 < MinTracks*1) Or (tot*1 > MaxTracks*1) Then If tot*1 < MinTracks*1 Then str = "(too few)" Else str = "(too many)" End If boo = False If Debug Then If TheGenre = "" Then Call out(ind&" has "&tot&" tracks above minimum rating "&str) Else Call out(ind&" has "&tot&" tracks above minimum rating and matching genres "&str) End If End If Else 'ensure the average is high enough str = "SELECT AVG(Rating) As AvgRating, MAX(PlayCounter) As MaxPlayed, MIN(DateAdded) As MinDate, AVG(PlayCounter) As AvgPlays, MAX(LastTimePlayed) As MaxTime FROM Songs WHERE IDAlbum="&ind Set iter = SDB.Database.OpenSQL(str) tot = FixLocale(iter.StringByName("AvgRating"))/20 If (tot*1 < AvgRating*1) Then boo = False If Debug Then Call out(ind&" has average rating of "&tot) Else 'ensure tracks are unplayed tot = iter.ValueByName("MaxPlayed") If (Unplayed) And (tot*1 > 0) Then boo = False If Debug Then Call out(ind&" has a track played "&tot&" times") Else 'ensure the playcount average is not too high tot = FixLocale(iter.StringByName("AvgPlays")) If tot*1 > AvgPlays*1 Then boo = False If Debug Then Call out(ind&" has tracks on average played "&tot&" times") Else 'ensure not before earliest days str = iter.StringByName("MinDate") tot = DateDiff("d",DateAdd("d",(Left(str,InStr(str,".")-1)-1),#01/01/1900#),Date()) If tot*1 > MaxDays*1 Then boo = False If Debug Then Call out(ind&" has a track added "&tot&" days ago") Else 'ensure the last played maximum is not too recent str = iter.StringByName("MaxTime") tot = DateDiff("d",DateAdd("d",(Left(str,InStr(str,".")-1)-1),#01/01/1900#),Date()) If tot*1 < MinDays*1 Then boo = False If Debug Then Call out(ind&" has a track played "&tot&" days ago") Else 'ensure not before earliest date If Not ( Earliest = "" ) Then str = iter.StringByName("MinDate") Dim days : days = Left(str,InStr(str,".")-1) Dim tmp : tmp = FixDate(DateAdd("d",(days-2),#01/01/1900#)) If tmp < Earliest Then boo = False If Debug Then Call out(ind&" has a track added "&tmp) End If End If End If End If End If End If End If End If End If 'ensure infinite loop doesn't occur SDB.ProcessMessages loops = loops + 1 If loops >= Attempts Then Exit Do End If Loop Until boo

'check album was found If boo Then 'add album to now playing Set list = SDB.NewSongList If Mode3f1 Then str = "AND (Songs.IDAlbum="&ind&") AND (Songs.Rating>"&minrat&") ORDER BY Random(*)" Set iter = SDB.Database.QuerySongs(str) For loops = 1 To ModeNum If iter.EOF Then Exit For End If If Debug Then Call out("Adding track: "&iter.Item.Title&" ("&iter.Item.ID&")") Call list.Add(iter.Item) Iter.Next Next Else str = "AND (Songs.IDAlbum="&ind&") AND (Songs.Rating>"&minrat&") " If ApplyFilter Then str = str&FilterSQL End If str = str&" ORDER BY CAST(Songs.DiscNumber AS INTEGER), CAST(Songs.TrackNumber AS INTEGER)" Set iter = SDB.Database.QuerySongs(str) Do While Not iter.EOF If Debug Then Call out("Adding track: "&iter.Item.Title&" ("&iter.Item.ID&")") Call list.Add(iter.Item) Iter.Next Loop End If

Sub OnOk(Btn) Dim Form : Set Form = SDB.Objects("AADJListForm") Dim Opts : Set Opts = SDB.Objects("AADJOptsForm") If (Opts Is Nothing) Or (Form Is Nothing) Then Set SDB.Objects("AADJListForm") = Nothing Exit Sub End If

Dim str : str = "" Dim doc : Set doc = Form.Common.ChildControl("WB").Interf.Document Dim Iter : Set Iter = SDB.Database.OpenSQL("SELECT IDGenre,GenreName FROM Genres ORDER BY GenreName") While Not Iter.EOF Dim chk : Set chk = doc.getElementById(Iter.StringByIndex(0)) If Not (chk Is Nothing) Then If chk.checked Then If str = "" Then str = Iter.StringByIndex(1) Else str = str&","&Iter.StringByIndex(1) End If End If End If Iter.Next WEnd

Function FixLocale(rat) Dim a : a = "0123456789" Dim i : i = 0 Dim r : r = "" If InStr(rat,"-") = 0 Then For i = 1 To Len(rat) If InStr(a,Mid(rat,i,1)) = 0 Then r = Left(rat,i-1) Exit For End If Next End If If r = "" Then FixLocale = -20 Else FixLocale = Int(r) End If End Function

Function GetFilterID(Name) If SDB.Database.OpenSQL("SELECT COUNT(*) FROM Filters WHERE Name='"& Replace(Name,"'","''") &"'").ValueByIndex(0) = 0 Then If SDB.MessageBox("The used Filter named ''"& Name &"'' does not any longer exist." & VbNewLine &_ "Do you wish to create the Report anyway without any Filter applied?" & VbNewLine &_ "(Click No to apply an existing Filter and run the Report again.)", mtWarning, Array(mbYes,mbNo)) = mrYes Then FilterName = "- No Filter -" SDB.IniFile.StringValue("AdvancedReport","FilterName") = FilterName GetFilterID = -1 Else Exit Function End If Else GetFilterID = SDB.Database.OpenSQL("SELECT ID FROM Filters WHERE Name='"& Replace(Name,"'","''") &"'").ValueByIndex(0) End IfEnd Function

Function GetFiltersArrayID(Name) Dim i,exists exists = False For i=0 To Ubound(Filters) If Filters(i)=Name Then GetFiltersArrayID=i exists = True Exit For End If Next If not exists Then GetFiltersArrayID=0End Function

Could you please give me a hand as I am having trouble installing script ... what do you mean with "and set the order appropriately" as I think this one is causing me trouble ... when I try to turn it on in tools menu I get error message.

trixmoto wrote:This is designed to work a bit like AutoDJ, but it queues entire albums at once. There is a menu option ("Tools, Scripts, Auto Album DJ (On/Off)") which allows you to switch this feature on/off. When switched on, whenever you play the last track in your now playing list (n.b. turn shuffle off!) a random album will appear (in track order) at the end of your list. This means you will never run out of albums to listen to.

This does not work because it assumes that IDAlbum <= IDAlbum for all albums. This is not true if you remove albums...