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