Auto/SelectionOfTheDay.vbs

Download and get help for different MediaMonkey for Windows 4 Addons.

Moderators: Peke, Gurus

Octopod
Posts: 463
Joined: Tue Jun 10, 2003 9:09 am

Auto/SelectionOfTheDay.vbs

Post by Octopod »

Hi monkeys,

You own a huge collection of tracks?
And sometimes you do not want to choose what to listen to?

I propose as a script a new MM functionality i called "Selection of the Day".
Each time you start MM, an album, an artist and a bunch of independent tracks are randomly selected and stored under a specific tree node (namely "Today...").

A "forced mode" can be enabled to re-randomize the selection of the day. See the comments within the script code.

I hope you will enjoy it as i enjoyed writting it. 8)

Octopod

Code: Select all

' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
'
'                  "Auto/SelectionOfTheDay.vbs", Sept-01-2004
'         VBScript for MediaMonkey 2.2.2 (or above), written by Octopod
'
'
' Purpose: 
' - This script adds a new tree node named "Today..." after the Playlists one
' - Each time you start MediaMonkey: an artist, an album and 14 songs (default)
'   are randomly selected from the entire DB
' - If you don't like the selection of the day, a toolbar icon can be enabled to
'   re-randomize it ("Standard" toolbar)
' - If you do not change the used strings, this script is localization-compatible
' - Hope you will enjoy it!
'
' Known issue:
' - If a "today sub-node" is already selected when randomize is manually forced:
'   1. Bad node selection refresh 
'   2. Tracks list is not updated (need to hit 'F5' or change node)
'
' Notes:
' - This script uses registry keys because i did not succeeded in retrieving a
'   global value from the nodes callbacks
' - This script may be not or badly optimized as i do not know VBS
' - So, and although this script should be safe, USE AT YOUR OWN RISK
'
' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


' --> Number of songs you want to be proposed?
NbSongs = 14

' --> Expand the node at startup?
ExpandNewNode = False

' --> Explicit nodes caption? 
ExplicitCaption = True

' --> Enable re-randomize?
EnableReRandomize = False


' Globals (cardinals)
Dim ArtistsCardinal
Dim AlbumsCardinal
Dim SongsCardinal

' Globals (random positions)
Dim SelectedArtistPos
Dim SelectedAlbumPos
Dim SelectedSongsPos()


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Sub OnStartup

  Dim Tree
  Set Tree = SDB.MainTree

  ' Main node
  Set Node_Today = Tree.CreateNode
  Node_Today.Caption = SDB.Localize("Today") + "..."
  Node_Today.IconIndex = 49
  Node_Today.UseScript = Script.ScriptPath
  Tree.AddNode Tree.Node_Playlists, Node_Today, 1
  SDB.Objects("Node_Today") = Node_Today

  ' Subnode 1 (Artist)
  Set Node_TodayArtist = Tree.CreateNode
  Node_TodayArtist.Caption = SDB.Localize("Artist") 
  Node_TodayArtist.IconIndex = 0
  Node_TodayArtist.UseScript = Script.ScriptPath
  Node_TodayArtist.OnFillTracksFunct = "FillTodayArtist"
  Tree.AddNode Node_Today, Node_TodayArtist, 3
  SDB.Objects("Node_TodayArtist") = Node_TodayArtist

  ' Subnode 2 (Album)
  Set Node_TodayAlbum = Tree.CreateNode
  Node_TodayAlbum.Caption = SDB.Localize("Album")
  Node_TodayAlbum.IconIndex = 16
  Node_TodayAlbum.UseScript = Script.ScriptPath
  Node_TodayAlbum.OnFillTracksFunct = "FillTodayAlbum"
  Tree.AddNode Node_Today, Node_TodayAlbum, 3
  SDB.Objects("Node_TodayAlbum")  = Node_TodayAlbum
  
  ' Subnode 3 (Songs)
  Set Node_TodaySong = Tree.CreateNode
  If (ExplicitCaption) Then
    Node_TodaySong.Caption = "(" + CStr(NbSongs) + " " + SDB.Localize("tracks") + ")"
  Else
    Node_TodaySong.Caption = SDB.Localize("Title")
  End If
  Node_TodaySong.IconIndex = 3
  Node_TodaySong.UseScript = Script.ScriptPath
  Node_TodaySong.OnFillTracksFunct = "FillTodaySong"
  Tree.AddNode Node_Today, Node_TodaySong, 3

  If (EnableReRandomize) Then
    ' Add a button to the Standard toolbar...
    Set UI = SDB.UI
    UI.AddMenuItemSep UI.Menu_TbStandard, -1, 0
    Set Mnu = UI.AddMenuItem( UI.Menu_TbStandard, 0, 0)
    Mnu.UseScript = Script.ScriptPath
    Mnu.OnClickFunc = "ForceNewSelection"
    Mnu.IconIndex = 45
    Mnu.Hint = SDB.Localize("Today") + "..."
  End If
  
  ' Prepare selection at startup
  Node_Today.Expanded = ExpandNewNode
  NewSelection(dummy)

End Sub


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Sub NewSelection(arg)

  InitCardinals
  InitSelections
  
  ' Explicit caption
  If (ExplicitCaption) Then

    Dim ID

    ' Artist
    Set Node_TodayArtist = SDB.Objects("Node_TodayArtist")
    ID = GetArtistID(SelectedArtistPos)
    Set ArtistName = SDB.Database.OpenSQL("SELECT Artists.Artist FROM Artists WHERE Artists.ID = " + CStr(ID))
    Node_TodayArtist.Caption = ArtistName.StringByIndex(0)
    
    ' Album
    Set Node_TodayAlbum = SDB.Objects("Node_TodayAlbum")
    ID = GetAlbumID(SelectedAlbumPos)
    Set AlbumName = SDB.Database.OpenSQL("SELECT Albums.Album FROM Albums WHERE Albums.ID = " + CStr(ID))
    Node_TodayAlbum.Caption = AlbumName.StringByIndex(0)

  End If

End Sub


Sub ForceNewSelection(arg)

  NewSelection(arg)
  
  ' Force explicit captions refresh
  If (EnableReRandomize) Then
    ' Main node
    Set Node_Today = SDB.Objects("Node_Today")
    If (Node_Today.Expanded) Then
      Node_Today.Expanded = False
      Node_Today.Expanded = True
    End If
  End If

End Sub


Sub InitCardinals

  ' Artists (remove ID = 0 = "Unknown" artist)
  Set AllArtists = SDB.Database.OpenSQL("SELECT COUNT(*) FROM Artists WHERE Artists.ID <> 0")
  ArtistsCardinal = AllArtists.StringByIndex(0)

  ' Albums (remove ID = 0 = "Unknown" album)
  Set AllAlbums = SDB.Database.OpenSQL("SELECT COUNT(*) FROM Albums WHERE Albums.ID <> 0")
  AlbumsCardinal = AllAlbums.StringByIndex(0)

  ' Songs
  Set AllSongs = SDB.Database.OpenSQL("SELECT COUNT(*) FROM Songs")
  SongsCardinal = AllSongs.StringByIndex(0)

End Sub


Sub InitSelections

  Randomize

  ' Randomly select a **position** within artists IDs records
  SelectedArtistPos = Int(Rnd * ArtistsCardinal)

  ' Randomly select **position** within albums IDs records
  SelectedAlbumPos = Int(Rnd * AlbumsCardinal)
  
  ' Randomly select "NbSongs" **positions** within songs IDs records
  ReDim SelectedSongsPos(NbSongs)
  For i = 1 to NbSongs
    SelectedSongsPos(i) = Int(Rnd * SongsCardinal)
  Next
  
  ' Bubble sort
  ' (sort positions to speed up songs display; only one loop)
  Dim count, i, j, temp 
  count = UBound(SelectedSongsPos, 1)
  For j = 0 To NbSongs - 1
    For i = 0 To NbSongs - 1
      If SelectedSongsPos(i) > SelectedSongsPos(i + 1) Then
        temp = SelectedSongsPos(i + 1)
        SelectedSongsPos(i + 1) = SelectedSongsPos(i)
        SelectedSongsPos(i) = temp
      End If
    Next
  Next

  ' Save selections
  Set Regs = SDB.Registry
  If Regs.OpenKey("SelectionOfTheDay", True) Then
    Regs.IntValue("SelectedArtistPos") = SelectedArtistPos
    Regs.IntValue("SelectedAlbumPos") = SelectedAlbumPos
    For i = 1 to NbSongs
      Regs.IntValue("SelectedSongsPos" + CStr(i)) = SelectedSongsPos(i)
    Next
    Regs.IntValue("SongsCardinal") = SongsCardinal
    Regs.CloseKey
  End If

End Sub


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Function GetArtistID(ArtistPos)

  ' Get all available artists IDs
  ' (This query is done in the callback to avoid slowing startup)
  Set ArtistsIDs = SDB.Database.OpenSQL("SELECT Artists.ID FROM Artists WHERE Artists.ID <> 0")

  ' Get its related **artist ID** (as position <> ID):

  ' 1. Forward to ArtistPos'th record
  For j = 1 to ArtistPos - 1
        ArtistsIDs.Next
  Next

  ' 2. Then get its related artist ID
  GetArtistID = ArtistsIDs.StringByIndex(0)

End Function


Sub FillTodayArtist(Node)

  ' Get back selected artist position
  Set Regs = SDB.Registry
  If Regs.OpenKey("SelectionOfTheDay", True) Then
    SelectedArtistPos = Regs.IntValue("SelectedArtistPos")
    Regs.CloseKey
  End If

  ' Get back related artist ID
  Dim ID
  ID = GetArtistID(SelectedArtistPos)

  ' Prepare query
  Dim ArtistQuery
  ArtistQuery = "AND Songs.IDArtist = "
  ArtistQuery = ArtistQuery + CStr(ID)
  ArtistQuery = ArtistQuery + " ORDER BY Songs.Year DESC, Songs.IDAlbum, Songs.SongOrder"

  ' Now, invoke query and update tracks list
  Set Tracks = SDB.MainTracksWindow
  Tracks.AddTracksFromQuery ArtistQuery
  Tracks.FinishAdding

End Sub


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Function GetAlbumID(AlbumPos)

  ' Get all available albums IDs
  ' (This query is done in the callback to avoid slowing startup)
  Set AlbumsIDs = SDB.Database.OpenSQL("SELECT Albums.ID FROM Albums WHERE Albums.ID <> 0")

  ' Get its related **album ID** (as position <> ID):

  ' 1. Forward to SelectedAlbumPos'th record
  For j = 1 to SelectedAlbumPos - 1
        AlbumsIDs.Next
  Next

  ' 2. Then get its related album ID
  GetAlbumID = AlbumsIDs.StringByIndex(0)
    
End Function


Sub FillTodayAlbum(Node)

  ' Get back selected album position
  Set Regs = SDB.Registry
  If Regs.OpenKey("SelectionOfTheDay", True) Then
    SelectedAlbumPos = Regs.IntValue("SelectedAlbumPos")
    Regs.CloseKey
  End If

  ' Get back related album ID
  Dim ID
  ID = GetAlbumID(SelectedAlbumPos)

  ' Prepare query
  Dim AlbumQuery
  AlbumQuery = "AND Songs.IDAlbum = "
  AlbumQuery = AlbumQuery + CStr(ID)
  AlbumQuery = AlbumQuery + " ORDER BY Songs.SongOrder"

  ' Now, invoke query and update tracks list
  Set Tracks = SDB.MainTracksWindow
  Tracks.AddTracksFromQuery AlbumQuery
  Tracks.FinishAdding

End Sub


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Sub FillTodaySong(Node)

  ReDim SelectedSongsPos(NbSongs)

  Dim tempo
  tempo = ""

  ' Get back selected songs positions
  Set Regs = SDB.Registry
  If Regs.OpenKey("SelectionOfTheDay", True) Then
    For i = 1 to NbSongs
      SelectedSongsPos(i) = Regs.IntValue("SelectedSongsPos" + CStr(i))
      tempo = tempo + CStr(SelectedSongsPos(i)) + " / "
    Next
    SongsCardinal = Regs.IntValue("SongsCardinal")
    Regs.CloseKey
  End If

  ' Get all available songs IDs
  ' (This query is done in the callback to avoid slowing startup)
  Set SongsIDs = SDB.Database.OpenSQL("SELECT Songs.ID FROM Songs ORDER BY Songs.ID")

  ' Store IDs
  ReDim IDList(NbSongs)
  Dim IDCardinal
  IDCardinal = 0

  ' Current position counter
  Dim NextID
  NextID = 1
  
  ' Forward along the iterator
  ' (This block is a bit complex but its aim is to avoid browsing the iterator
  '  several times, so display of the selected tracks will be faster)
  For j = 0 to (SongsCardinal - 1)

    ' Check if the current item is selected ("SelectedSongsPos" is sorted)
    If (j = SelectedSongsPos(NextID)) Then

      IDList(IDCardinal) = SongsIDs.StringByIndex(0)
      IDCardinal = IDCardinal + 1

      ' Break loop if all IDs already found
      If (NextID = NbSongs) Then
        Exit For
      End If

      ' Otherwise, update next position to reach
      NextID = NextID + 1
      
      ' Check if douboons in the subsequent cells
      If (NextID < NbSongs) Then
        While (SelectedSongsPos(NextID) = SelectedSongsPos(NextID + 1))
          NextID = NextID + 1
        WEnd
      End If
        
    End If
    
    ' Continue...
    SongsIDs.Next
  
  Next
  
  ' Prepare query
  Dim SongQuery
  SongQuery = "AND Songs.ID IN ("
  For i = 0 to (IDCardinal - 2)
    SongQuery = SongQuery + CStr(IDList(i)) + ", "
  Next
  SongQuery = SongQuery + CStr(IDList(IDCardinal - 1)) + ") ORDER BY Songs.SongTitle"
  
  ' Now, invoke query and update tracks list
  Set Tracks = SDB.MainTracksWindow
  Tracks.AddTracksFromQuery SongQuery
  Tracks.FinishAdding

  ' Because of possible doubloons and/or ghost songs IDs, 
  ' less than "NbSongs" songs might be proposed

End Sub
jiri
Posts: 5417
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Post by jiri »

Nice idea and implementation! I'm glad that the new scripting API is getting useful!

Jiri
b0b0b0b-guest

Post by b0b0b0b-guest »

good job octopod
pah68
Posts: 1504
Joined: Wed Apr 07, 2004 5:26 pm
Location: Sydney, Australia

Post by pah68 »

How do I use it? :-?
Octopod
Posts: 463
Joined: Tue Jun 10, 2003 9:09 am

Post by Octopod »

You must have MM 2.2.2 Gold version.

So simply copy/paste the script code to 'program files/mediamonkey/Scripts/Auto/SelectionOfTheDay.vbs'. Then close MM if it is open and restart it!
llwb

octopod, is it possible...

Post by llwb »

Neat idea octopod. What I wonder is if it can be customized so that the "Selection of the Day" consists only of mp3s actually on the computer. It doesn't seem very practical given the concept of the script to have to insert CDs.
TIA
llwb[/u]
Octopod
Posts: 463
Joined: Tue Jun 10, 2003 9:09 am

Re: octopod, is it possible...

Post by Octopod »

llwb wrote:It doesn't seem very practical given the concept of the script to have to insert CDs.
CD? What's that?!?! :wink:

Well, you're right. Actually i do not handle CD anymore so i did not think to this kind of problem!

(All my physical CDs (and DVDs) are ripped to a dedicated HD and the computer is connected both to the speakers, an amplifier and TV-OUT; all is managed by a remote control...)

I think this is possible though but all the queries would have to be modified in order to check "SongPath" from the Songs table (will be more complex as a lot of tables will have to be joint). Will try to have a look to this at the end of the week.
Lowlander
Posts: 56465
Joined: Sat Sep 06, 2003 5:53 pm
Location: MediaMonkey 5

Unrelated

Post by Lowlander »

What remote control setup do you use to control your PC?
Octopod
Posts: 463
Joined: Tue Jun 10, 2003 9:09 am

Post by Octopod »

Here is what it looks like (i don't speak german myself...):
http://www.pearl.de/p/PE4444-Q-Sonic-Ma ... rarot.html
http://www.pearl.fr/article-PE4444.html

I also bought a shareware named uICE (Universal Infrared Control Engine) to program it according to the active software.

I love it! 8)
Lowlander
Posts: 56465
Joined: Sat Sep 06, 2003 5:53 pm
Location: MediaMonkey 5

Post by Lowlander »

Luckily I read German, very nice as it not only can control your PC as well as your other equipment.

thx
TheRocket
Posts: 84
Joined: Sat Jan 17, 2004 4:42 pm
Location: Quebec, Canada

Lol

Post by TheRocket »

It seems to me that all remotes look like others :-)

This one is identical to the RemoteWonder by Ati...
And look like the Nvidia remonte :-)

Who is really making those remotes??? Anyway :-)
TheRocket
-- MMWBE 0.91 released! Get it here http://rocket.dyndns.info/mp3manager/mmwbe_v091.rar or see topic http://www.mediamonkey.com/forum/viewto ... 4005#34005 for more details!!!
Lowlander
Posts: 56465
Joined: Sat Sep 06, 2003 5:53 pm
Location: MediaMonkey 5

Post by Lowlander »

Often third parties. I own two TV's from different (I believe unrelated) brands and they have nearly identical remotes (some different buttons). This allows them to be cheap probably. Anyway the remote is often not the functionality your looking for, it accesses the functionality.
Octopod
Posts: 463
Joined: Tue Jun 10, 2003 9:09 am

Post by Octopod »

llwb,

Here is a version i tested a bit.

It uses as distinguishing criteria: "Songs.AudioCDTrack <= 0".
This should not be enough as you can set all your CD tracks order to 0...

In this case try replacing all "Songs.AudioCDTrack <= 0" instances by "Songs.SongPath NOT LIKE '%.cda'" (that should be a bit slower but better though).

Tell if it's ok now!

Octopod

Code: Select all

' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 
' 
'                  "Auto/SelectionOfTheDay.vbs", Sept-01-2004 
'         VBScript for MediaMonkey 2.2.2 (or above), written by Octopod 
' 
' 
' Purpose: 
' - This script adds a new tree node named "Today..." after the Playlists one 
' - Each time you start MediaMonkey: an artist, an album and 14 songs (default) 
'   are randomly selected from the entire DB 
' - If you don't like the selection of the day, a toolbar icon can be enabled to 
'   re-randomize it ("Standard" toolbar) 
' - If you do not change the used strings, this script is localization-compatible 
' - Hope you will enjoy it! 
' 
' Known issue: 
' - If a "today sub-node" is already selected when randomize is manually forced: 
'   1. Bad node selection refresh 
'   2. Tracks list is not updated (need to hit 'F5' or change node) 
' 
' Notes: 
' - This script uses registry keys because i did not succeeded in retrieving a 
'   global value from the nodes callbacks 
' - This script may be not or badly optimized as i do not know VBS 
' - So, and although this script should be safe, USE AT YOUR OWN RISK 
' 
' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


' --> Number of songs you want to be proposed? 
NbSongs = 14 

' --> Expand the node at startup? 
ExpandNewNode = False 

' --> Explicit nodes caption? 
ExplicitCaption = True 

' --> Enable re-randomize? 
EnableReRandomize = False 

' --> Forget CDs?
ForgetCD = True


' Globals (cardinals) 
Dim ArtistsCardinal 
Dim AlbumsCardinal 
Dim SongsCardinal 

' Globals (random positions) 
Dim SelectedArtistPos 
Dim SelectedAlbumPos 
Dim SelectedSongsPos() 


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Sub OnStartup 

  Dim Tree 
  Set Tree = SDB.MainTree 

  ' Main node 
  Set Node_Today = Tree.CreateNode 
  Node_Today.Caption = "TODAY (NO CD)" 'SDB.Localize("Today") + "..." 
  Node_Today.IconIndex = 49 
  Node_Today.UseScript = Script.ScriptPath 
  Tree.AddNode Tree.Node_Playlists, Node_Today, 1 
  SDB.Objects("Node_Today") = Node_Today 

  ' Subnode 1 (Artist) 
  Set Node_TodayArtist = Tree.CreateNode 
  Node_TodayArtist.Caption = SDB.Localize("Artist") 
  Node_TodayArtist.IconIndex = 0 
  Node_TodayArtist.UseScript = Script.ScriptPath 
  Node_TodayArtist.OnFillTracksFunct = "FillTodayArtist" 
  Tree.AddNode Node_Today, Node_TodayArtist, 3 
  SDB.Objects("Node_TodayArtist") = Node_TodayArtist 

  ' Subnode 2 (Album) 
  Set Node_TodayAlbum = Tree.CreateNode 
  Node_TodayAlbum.Caption = SDB.Localize("Album") 
  Node_TodayAlbum.IconIndex = 16 
  Node_TodayAlbum.UseScript = Script.ScriptPath 
  Node_TodayAlbum.OnFillTracksFunct = "FillTodayAlbum" 
  Tree.AddNode Node_Today, Node_TodayAlbum, 3 
  SDB.Objects("Node_TodayAlbum")  = Node_TodayAlbum 
  
  ' Subnode 3 (Songs) 
  Set Node_TodaySong = Tree.CreateNode 
  If (ExplicitCaption) Then 
    Node_TodaySong.Caption = "(" + CStr(NbSongs) + " " + SDB.Localize("tracks") + ")" 
  Else 
    Node_TodaySong.Caption = SDB.Localize("Title") 
  End If 
  Node_TodaySong.IconIndex = 3 
  Node_TodaySong.UseScript = Script.ScriptPath 
  Node_TodaySong.OnFillTracksFunct = "FillTodaySong" 
  Tree.AddNode Node_Today, Node_TodaySong, 3 

  If (EnableReRandomize) Then 
    ' Add a button to the Standard toolbar... 
    Set UI = SDB.UI 
    UI.AddMenuItemSep UI.Menu_TbStandard, -1, 0 
    Set Mnu = UI.AddMenuItem( UI.Menu_TbStandard, 0, 0) 
    Mnu.UseScript = Script.ScriptPath 
    Mnu.OnClickFunc = "ForceNewSelection" 
    Mnu.IconIndex = 45 
    Mnu.Hint = SDB.Localize("Today") + "..." 
  End If 
  
  ' Prepare selection at startup 
  Node_Today.Expanded = ExpandNewNode 
  NewSelection(dummy) 

End Sub 


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Sub NewSelection(arg) 

  InitCardinals 
  InitSelections 
  
  ' Explicit caption 
  If (ExplicitCaption) Then 

    Dim ID 

    ' Artist 
    Set Node_TodayArtist = SDB.Objects("Node_TodayArtist") 
    ID = GetArtistID(SelectedArtistPos) 
    Set ArtistName = SDB.Database.OpenSQL("SELECT Artists.Artist FROM Artists WHERE Artists.ID = " + CStr(ID)) 
    Node_TodayArtist.Caption = ArtistName.StringByIndex(0) 
    
    ' Album 
    Set Node_TodayAlbum = SDB.Objects("Node_TodayAlbum") 
    ID = GetAlbumID(SelectedAlbumPos) 
    Set AlbumName = SDB.Database.OpenSQL("SELECT Albums.Album FROM Albums WHERE Albums.ID = " + CStr(ID)) 
    Node_TodayAlbum.Caption = AlbumName.StringByIndex(0) 

  End If 

End Sub 


Sub ForceNewSelection(arg) 

  NewSelection(arg) 
  
  ' Force explicit captions refresh 
  If (EnableReRandomize) Then 
    ' Main node 
    Set Node_Today = SDB.Objects("Node_Today") 
    If (Node_Today.Expanded) Then 
      Node_Today.Expanded = False 
      Node_Today.Expanded = True 
    End If 
  End If 

End Sub 


Sub InitCardinals 

  If (ForgetCD) Then
  
    ' Artists (remove ID = 0 = "Unknown" artist) 
    Set AllArtists = SDB.Database.OpenSQL("SELECT COUNT(Artists.ID) FROM (SELECT DISTINCTROW Artists.ID FROM (artists INNER JOIN Songs ON artists.ID = Songs.IDArtist) WHERE artists.ID <> 0 AND Songs.AudioCDTrack <= 0)") 
    ArtistsCardinal = AllArtists.StringByIndex(0) 

    ' Albums (remove ID = 0 = "Unknown" album) 
    Set AllAlbums = SDB.Database.OpenSQL("SELECT COUNT(*) FROM (SELECT DISTINCTROW Albums.ID FROM (albums INNER JOIN Songs ON albums.ID = Songs.IDAlbum) WHERE albums.ID <> 0 AND Songs.AudioCDTrack <= 0)") 
    AlbumsCardinal = AllAlbums.StringByIndex(0) 

    ' Songs 
    Set AllSongs = SDB.Database.OpenSQL("SELECT COUNT(*) FROM Songs WHERE Songs.AudioCDTrack <= 0")
    SongsCardinal = AllSongs.StringByIndex(0) 
  
  Else

    ' Artists (remove ID = 0 = "Unknown" artist) 
    Set AllArtists = SDB.Database.OpenSQL("SELECT COUNT(*) FROM Artists WHERE Artists.ID <> 0") 
    ArtistsCardinal = AllArtists.StringByIndex(0) 

    ' Albums (remove ID = 0 = "Unknown" album) 
    Set AllAlbums = SDB.Database.OpenSQL("SELECT COUNT(*) FROM Albums WHERE Albums.ID <> 0") 
    AlbumsCardinal = AllAlbums.StringByIndex(0) 

    ' Songs 
    Set AllSongs = SDB.Database.OpenSQL("SELECT COUNT(*) FROM Songs") 
    SongsCardinal = AllSongs.StringByIndex(0) 
  
  End If

End Sub 


Sub InitSelections 

  Randomize 

  ' Randomly select a **position** within artists IDs records 
  SelectedArtistPos = Int(Rnd * ArtistsCardinal) 

  ' Randomly select **position** within albums IDs records 
  SelectedAlbumPos = Int(Rnd * AlbumsCardinal) 
  
  ' Randomly select "NbSongs" **positions** within songs IDs records 
  ReDim SelectedSongsPos(NbSongs) 
  For i = 1 to NbSongs 
    SelectedSongsPos(i) = Int(Rnd * SongsCardinal) 
  Next 
  
  ' Bubble sort 
  ' (sort positions to speed up songs display; only one loop) 
  Dim count, i, j, temp 
  count = UBound(SelectedSongsPos, 1) 
  For j = 0 To NbSongs - 1 
    For i = 0 To NbSongs - 1 
      If SelectedSongsPos(i) > SelectedSongsPos(i + 1) Then 
        temp = SelectedSongsPos(i + 1) 
        SelectedSongsPos(i + 1) = SelectedSongsPos(i) 
        SelectedSongsPos(i) = temp 
      End If 
    Next 
  Next 

  ' Save selections 
  Set Regs = SDB.Registry 
  If Regs.OpenKey("SelectionOfTheDay", True) Then 
    Regs.IntValue("SelectedArtistPos") = SelectedArtistPos 
    Regs.IntValue("SelectedAlbumPos") = SelectedAlbumPos 
    For i = 1 to NbSongs 
      Regs.IntValue("SelectedSongsPos" + CStr(i)) = SelectedSongsPos(i) 
    Next 
    Regs.IntValue("SongsCardinal") = SongsCardinal 
    Regs.CloseKey 
  End If 

End Sub 


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Function GetArtistID(ArtistPos) 

  ' Get all available artists IDs 
  ' (This query is done in the callback to avoid slowing startup) 

  If (ForgetCD) Then
  
    Set ArtistsIDs = SDB.Database.OpenSQL("SELECT Artists.ID FROM (artists INNER JOIN Songs ON artists.ID = Songs.IDArtist) WHERE artists.ID <> 0 AND Songs.AudioCDTrack <= 0")
  
  Else

    Set ArtistsIDs = SDB.Database.OpenSQL("SELECT Artists.ID FROM Artists WHERE Artists.ID <> 0") 
    
  End If

  ' Get its related **artist ID** (as position <> ID): 

  ' 1. Forward to ArtistPos'th record 
  For j = 1 to ArtistPos - 1 
        ArtistsIDs.Next 
  Next 

  ' 2. Then get its related artist ID 
  GetArtistID = ArtistsIDs.StringByIndex(0) 

End Function 


Sub FillTodayArtist(Node) 

  ' Get back selected artist position 
  Set Regs = SDB.Registry 
  If Regs.OpenKey("SelectionOfTheDay", True) Then 
    SelectedArtistPos = Regs.IntValue("SelectedArtistPos") 
    Regs.CloseKey 
  End If 

  ' Get back related artist ID 
  Dim ID 
  ID = GetArtistID(SelectedArtistPos) 

  ' Prepare query 
  Dim ArtistQuery 
  ArtistQuery = "AND Songs.IDArtist = " 
  ArtistQuery = ArtistQuery + CStr(ID) 
  ArtistQuery = ArtistQuery + " ORDER BY Songs.Year DESC, Songs.IDAlbum, Songs.SongOrder" 

  ' Now, invoke query and update tracks list 
  Set Tracks = SDB.MainTracksWindow 
  Tracks.AddTracksFromQuery ArtistQuery 
  Tracks.FinishAdding 

End Sub 


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Function GetAlbumID(AlbumPos) 

  ' Get all available albums IDs 
  ' (This query is done in the callback to avoid slowing startup) 
  
  If (ForgetCD) Then
  
    Set AlbumsIDs = SDB.Database.OpenSQL("SELECT Albums.ID FROM (albums INNER JOIN Songs ON albums.ID = Songs.IDAlbum) WHERE albums.ID <> 0 AND Songs.AudioCDTrack <= 0")
    
  Else
  
    Set AlbumsIDs = SDB.Database.OpenSQL("SELECT Albums.ID FROM Albums WHERE Albums.ID <> 0") 
    
  End If

  ' Get its related **album ID** (as position <> ID): 

  ' 1. Forward to SelectedAlbumPos'th record 
  For j = 1 to SelectedAlbumPos - 1 
        AlbumsIDs.Next 
  Next 

  ' 2. Then get its related album ID 
  GetAlbumID = AlbumsIDs.StringByIndex(0) 
    
End Function 


Sub FillTodayAlbum(Node) 

  ' Get back selected album position 
  Set Regs = SDB.Registry 
  If Regs.OpenKey("SelectionOfTheDay", True) Then 
    SelectedAlbumPos = Regs.IntValue("SelectedAlbumPos") 
    Regs.CloseKey 
  End If 

  ' Get back related album ID 
  Dim ID 
  ID = GetAlbumID(SelectedAlbumPos) 

  ' Prepare query 
  Dim AlbumQuery 
  AlbumQuery = "AND Songs.IDAlbum = " 
  AlbumQuery = AlbumQuery + CStr(ID) 
  AlbumQuery = AlbumQuery + " ORDER BY Songs.SongOrder" 

  ' Now, invoke query and update tracks list 
  Set Tracks = SDB.MainTracksWindow 
  Tracks.AddTracksFromQuery AlbumQuery 
  Tracks.FinishAdding 

End Sub 


' ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- 


Sub FillTodaySong(Node) 

  ReDim SelectedSongsPos(NbSongs) 

  Dim tempo 
  tempo = "" 

  ' Get back selected songs positions 
  Set Regs = SDB.Registry 
  If Regs.OpenKey("SelectionOfTheDay", True) Then 
    For i = 1 to NbSongs 
      SelectedSongsPos(i) = Regs.IntValue("SelectedSongsPos" + CStr(i)) 
      tempo = tempo + CStr(SelectedSongsPos(i)) + " / " 
    Next 
    SongsCardinal = Regs.IntValue("SongsCardinal") 
    Regs.CloseKey 
  End If 

  ' Get all available songs IDs 
  ' (This query is done in the callback to avoid slowing startup)
  
  If (ForgetCD) Then

    Set SongsIDs = SDB.Database.OpenSQL("SELECT Songs.ID FROM Songs WHERE Songs.AudioCDTrack <= 0 ORDER BY Songs.ID") 
    
  Else 
  
    Set SongsIDs = SDB.Database.OpenSQL("SELECT Songs.ID FROM Songs ORDER BY Songs.ID") 
    
  End If
  
  ' Store IDs 
  ReDim IDList(NbSongs) 
  Dim IDCardinal 
  IDCardinal = 0 

  ' Current position counter 
  Dim NextID 
  NextID = 1 
  
  ' Forward along the iterator 
  ' (This block is a bit complex but its aim is to avoid browsing the iterator 
  '  several times, so display of the selected tracks will be faster) 
  For j = 0 to (SongsCardinal - 1) 

    ' Check if the current item is selected ("SelectedSongsPos" is sorted) 
    If (j = SelectedSongsPos(NextID)) Then 

      IDList(IDCardinal) = SongsIDs.StringByIndex(0) 
      IDCardinal = IDCardinal + 1 

      ' Break loop if all IDs already found 
      If (NextID = NbSongs) Then 
        Exit For 
      End If 

      ' Otherwise, update next position to reach 
      NextID = NextID + 1 
      
      ' Check if douboons in the subsequent cells 
      If (NextID < NbSongs) Then 
        While (SelectedSongsPos(NextID) = SelectedSongsPos(NextID + 1)) 
          NextID = NextID + 1 
        WEnd 
      End If 
        
    End If 
    
    ' Continue... 
    SongsIDs.Next 
  
  Next 
  
  ' Prepare query 
  Dim SongQuery 
  SongQuery = "AND Songs.ID IN (" 
  For i = 0 to (IDCardinal - 2) 
    SongQuery = SongQuery + CStr(IDList(i)) + ", " 
  Next 
  SongQuery = SongQuery + CStr(IDList(IDCardinal - 1)) + ") ORDER BY Songs.SongTitle" 
  
  ' Now, invoke query and update tracks list 
  Set Tracks = SDB.MainTracksWindow 
  Tracks.AddTracksFromQuery SongQuery 
  Tracks.FinishAdding 

  ' Because of possible doubloons and/or ghost songs IDs, 
  ' less than "NbSongs" songs might be proposed 

End Sub 
Guest

Post by Guest »

Tell if it's ok now!

Thanks Octopod. But it is still displaying tracks from CDs. Here are the error messages I get--

Problem quereying the database:
42000:[Microsoft][ODBC] Microsoft Acess Driver Syntax Error in FROM clause
--------------------------------------------------------------

Error#-2147418113-DongsDB.SDBDBIterator
List Index out of bounds (0)
File:.....\SelectionOfTheDay.vbs Line:174, column 4
------------------------------------------------------------

Maybe I did not substitute the new text in the correct way?

llwb[/quote]
al_iguana

Post by al_iguana »

this is superb - just what i was looking for!

(using the first version because all my music is on my pc too)

thanks a million :D
Post Reply