Page 1 of 6

Randomise Playlist 1.3 - Updated 20/10/2010

Posted: Mon Nov 26, 2007 7:28 am
by trixmoto
This script creates a menu item which appears when right clicking on a playlist. It gives you the option to randomise this playlist.

Code: Select all

'
' MediaMonkey Script
'
' NAME: RandomisePlaylist 1.3
'
' AUTHOR: trixmoto (http://trixmoto.net)
' DATE : 20/10/2010
'
' INSTALL: Copy to Scripts\Auto directory and add the following to Scripts.ini 
'          Don't forget to remove comments (') and set the order appropriately
'
' [RandomisePlaylist]
' FileName=Auto\RandomisePlaylist.vbs
' ProcName=RandomisePlaylist
' Order=50
' DisplayName=Randomise Playlist
' Description=Randomises selected playlist
' Language=VBScript
' ScriptType=0 
'
' FIXES: Fixed playlist node being fully expanded on startup
' 

Option Explicit
Dim AutoMode : AutoMode = False 'Randomise playlists automatically

Sub OnStartup
  Call RegisterAutoEvents()
  If AutoMode Then
    Exit Sub
  End If
  
  Dim itm : Set itm = SDB.Objects("RandomisePlaylistMenu")
  If itm Is Nothing Then
    Set itm = SDB.UI.AddMenuItem(SDB.UI.Menu_Pop_Tree,1,0)
    itm.Caption = "Randomise Playlist"
    itm.OnClickFunc = "ItmClick"
    itm.UseScript = Script.ScriptPath
    itm.IconIndex = 25  
    itm.Visible = False
    Set SDB.Objects("RandomisePlaylistMenu") = itm
    Call Script.RegisterEvent(SDB,"OnChangedSelection","ShowMenu")
  End If
End Sub

Sub RegisterAutoEvents
  Dim dic : Set dic = SDB.Objects("RandomisePlaylistDic")
  If dic Is Nothing Then
    Set dic = CreateObject("Scripting.Dictionary")
  End If
  Dim tree : Set tree = SDB.MainTree
  Dim node : Set node = tree.Node_Playlists
  If node.Visible Then
    Dim exp : exp = node.Expanded
    node.Expanded = True
    Set node = tree.FirstChildNode(node)
    Call DoChildren(dic,node,":|:")
    If exp = False Then
      tree.Node_Playlists.Expanded = False
    End If
  End If
  Set SDB.Objects("RandomisePlaylistDic") = dic
End Sub

Sub DoChildren(dic,node,levs)
  Do While Not (node Is Nothing)
    If node.NodeType = 61 Then
      node.CustomData = levs
      Dim list : Set list = GetPlaylistFromNode(node)
      If Not (dic.Exists("#"&list.ID)) Then
        Call dic.Add("#"&list.ID,node.Caption)
        If AutoMode Then
          Call Script.RegisterEvent(node,"OnNodeFocused","ShowAuto")
        End If
      End If
    End If
    Dim tree : Set tree = SDB.MainTree
    If node.HasChildren Then
      Dim exp : exp = node.Expanded
      node.Expanded = True    
      Dim tmp : Set tmp = tree.FirstChildNode(node)
      Call DoChildren(dic,tmp,levs&node.Caption&":|:")
      If exp = False Then
        node.Expanded = False
      End If      
    End If
    Set node = tree.NextSiblingNode(node)
  Loop 
End Sub

Sub ShowAuto(node)
  Dim list : Set list = GetPlaylistFromNode(node)
  If Not (list Is Nothing) Then
    Dim w : Set w = SDB.MainTracksWindow
    Dim t : Set t = list.Tracks
    While t.Count > 0
      Dim n : n = Int(t.Count*Rnd)
      Call w.AddTrack(t.Item(n))
      Call t.Delete(n)
    WEnd
    Call w.FinishAdding()
  End If
End Sub

Sub ShowMenu()
  Dim itm : Set itm = SDB.Objects("RandomisePlaylistMenu")
  If Not (itm Is Nothing) Then  
    Dim vis : vis = False
    Dim node : Set node = SDB.MainTree.CurrentNode
    If Not (node Is Nothing) Then
      If node.NodeType = 61 Then
        vis = True
      End If
    End if
    itm.Visible = vis    
  End If
End Sub 

Sub ItmClick(i)
  Call RandomisePlaylist()
End Sub

Sub RandomisePlaylist()
  Dim tree : Set tree = SDB.MainTree
  Dim node : Set node = tree.CurrentNode
  If Not (node Is Nothing) Then
    If node.NodeType = 61 Then
      Dim list : Set list = GetPlaylistFromNode(node)
      If Not (list Is Nothing) Then
        Call DoRandomise(list)
        tree.CurrentNode = tree.ParentNode(node)
        tree.CurrentNode = node
        Exit Sub
      End If
    End If
  End If
  Call SDB.Messagebox("RandomisePlaylist: You must select a playlist node.",mtError,Array(mbOk))
End Sub

Sub DoRandomise(p)
  Dim t : Set t = p.Tracks
  p.Clear
  While t.Count > 0
    Dim n : n = Int(t.Count*Rnd)
    Call p.AddTrack(t.Item(n))
    Call t.Delete(n)
    SDB.ProcessMessages 
  WEnd
End Sub

Sub Install()
  Dim inip : inip = SDB.ApplicationPath&"Scripts\Scripts.ini"
  Dim inif : Set inif = SDB.Tools.IniFileByPath(inip)
  If Not (inif Is Nothing) Then
    inif.StringValue("RandomisePlaylist","Filename") = "Auto\RandomisePlaylist.vbs"
    inif.StringValue("RandomisePlaylist","Procname") = "RandomisePlaylist"
    inif.StringValue("RandomisePlaylist","Order") = "50"
    inif.StringValue("RandomisePlaylist","DisplayName") = "Randomise Playlist"
    inif.StringValue("RandomisePlaylist","Description") = "Randomises selected playlist"
    inif.StringValue("RandomisePlaylist","Language") = "VBScript"
    inif.StringValue("RandomisePlaylist","ScriptType") = "0"
    SDB.RefreshScriptItems
  End If
  Call OnStartup()
End Sub

Function GetPlaylistFromNode(node)
  Dim str : str = node.CustomData
  If InStr(str,":|:") = 0 Then 
    Set GetPlaylistFromNode = SDB.PlaylistByTitle(node.Caption)
  Else
    Set GetPlaylistFromNode = SDB.PlaylistByTitle("")
    Dim i,j,kids,kid
    Dim arr : arr = Split(str,":|:")
    For i = 1 To UBound(arr)-1
      Dim nam : nam = arr(i)
      Set kids = GetPlaylistFromNode.ChildPlaylists
      For j = 0 To kids.Count-1
        Set kid = kids.Item(j)
        If kid.Title = nam Then
          Set GetPlaylistFromNode = kid
          Exit For
        End If          
      Next
    Next    
    Set kids = GetPlaylistFromNode.ChildPlaylists
    For j = 0 To kids.Count-1
      Set kid = kids.Item(j)
      If kid.Title = node.Caption Then
        Set GetPlaylistFromNode = kid
        Exit For
      End If          
    Next
  End If
  If Not (GetPlaylistFromNode.Title = node.Caption) Then
    Set GetPlaylistFromNode = Nothing
  End If
End Function

Posted: Mon Nov 26, 2007 7:58 am
by nohitter151
Been waiting a long time for one like this, and it works great! Thanks for another great one!

Posted: Mon Nov 26, 2007 8:41 am
by bluesbeat
Just tried this in MM2.
Is it supposed to work on Auto Playlists?, can only see the option on normal playlists.

thanks,
Geoff

Posted: Mon Nov 26, 2007 11:12 am
by trixmoto
You cannot randomise and autoplaylist because they are automatic and are therefore rebuilt.

Posted: Mon Nov 26, 2007 11:20 am
by Bex
Well, you randomize an autoplaylist in its settings. So there's need to have a script for it (it's not even possible).

Posted: Mon Nov 26, 2007 5:43 pm
by bluesbeat
ok, thanks.

Posted: Thu Nov 29, 2007 7:22 am
by The Crow
Hello trixmoto! Thanks for your answer to my shuffle question in the other thread.

Is it perhaps possible, to modify the randomise 1.0 script so that the playlist gets randomised automatically when it is selected with the left mouse button so that you don't need the right click and select "Randomise Playlist"?

Posted: Thu Nov 29, 2007 7:46 am
by trixmoto
Yes this would be possible. I'll add it to my to do list! :)

Posted: Thu Nov 29, 2007 9:48 am
by The Crow
Great!

Posted: Thu Nov 29, 2007 3:50 pm
by Guest
Is it possible to make it work with the genre lists as well or is it limited to playlists?

Posted: Thu Nov 29, 2007 4:01 pm
by Teknojnky
why not create an auto-playlist of:

genre = whatever genre(s) you want
sort = random a-z

Posted: Fri Nov 30, 2007 4:46 am
by The Crow
Teknojnky wrote:why not create an auto-playlist of:
Using Auto DJ you have to choose a determined playlist where the next random tracks are selected from, but I would like to get randomised tracks from the list playing currently and I think there is no possibility to make such a setting with Auto DJ.

As far as I see an automatic comfortable randomised play back with tracks from the active playlist seems to be able to realize with

1. a script that shuffles the playlist already when it is clicked on
(this is on trixmoto's list :), but this probably wouldn't work with genre lists (?))

or 2. an improved MM random mode where at least the Now Playing action works properly (this is going to be fixed with MM 3.0 RC 4, http://www.mediamonkey.com/forum/viewtopic.php?t=21616; however, a navigation with "Next track", "Previous track" and the "Play selected tracks next" button wouldn't probably work)

or 3. with an improved Auto DJ that selects tracks from the actual playlist (perhaps you can realize it with Auto DJ Scripts).

Posted: Fri Nov 30, 2007 8:04 am
by nohitter151
The Crow wrote:
Teknojnky wrote:why not create an auto-playlist of:
Using Auto DJ you have to choose a determined playlist where the next random tracks are selected from, but I would like to get randomised tracks from the list playing currently and I think there is no possibility to make such a setting with Auto DJ.

As far as I see an automatic comfortable randomised play back with tracks from the active playlist seems to be able to realize with

1. a script that shuffles the playlist already when it is clicked on
(this is on trixmoto's list :), but this probably wouldn't work with genre lists (?))

or 2. an improved MM random mode where at least the Now Playing action works properly (this is going to be fixed with MM 3.0 RC 4, http://www.mediamonkey.com/forum/viewtopic.php?t=21616; however, a navigation with "Next track", "Previous track" and the "Play selected tracks next" button wouldn't probably work)

or 3. with an improved Auto DJ that selects tracks from the actual playlist (perhaps you can realize it with Auto DJ Scripts).
I'm pretty sure TechnoJnky was talking about Auto-Playlists, not Auto-DJ

Posted: Fri Nov 30, 2007 8:46 am
by The Crow
Okay, you're right, I missed that. Auto Playlist works, of course, but I'd like to get randomised manual playlists (and/or the genre list) that can't be really created with criterias of Auto Playlist.
I know, it's a little bit special, but I'm using MM in a flatshare with different users and want to get it as easy without running manual commands.

There are, as I wrote, several ways to solve this. The aim would also be achieved if it was possible to add tracks manually to the Auto Playlist. :o That sounds strange but would solve my problem, too. :wink:

The best way seems to be waiting for trixmotos new script.

Posted: Fri Nov 30, 2007 9:57 am
by Bex
The Crow wrote:The aim would also be achieved if it was possible to add tracks manually to the Auto Playlist. :o That sounds strange but would solve my problem, too. :wink:
In MM3 you can achieve that since autoplaylist can "link" from other playlists. e.g. A static playlist to which you can add tracks which then ends up in your autoplaylist.