Code: Select all
'
' MediaMonkey Script
'
' NAME: FixPlaylistDups 4.6
'
' AUTHOR: trixmoto (http://trixmoto.net)
' DATE : 08/02/2011
'
' INSTALL: Copy to Scripts directory and add the following to Scripts.ini
' Don't forget to remove comments (') and set the order appropriately
'
' FIXES: Fixed autoplaylists should be ignored
' Fixed recursion not working properly
'
' [FixPlaylistDups]
' FileName=FixPlaylistDups.vbs
' ProcName=FixPlaylistDups
' Order=9
' DisplayName=Fix Playlist Dups
' Description=Fix Playlist Duplications
' Language=VBScript
' ScriptType=0
'
Option Explicit
Function dup(track1,dupby,track2,cases)
dup = False
Select Case dupby
Case 0
If track1.ID = track2.ID Then
dup = true
End If
Case 1
If cases Then
If (track1.ArtistName = track2.ArtistName) And (track1.Title = track2.Title) Then
dup = true
End If
Else
If (UCase(track1.ArtistName) = UCase(track2.ArtistName)) And (UCase(track1.Title) = UCase(track2.Title)) Then
dup = true
End If
End If
Case 2
If (track1.LastPlayed = track2.LastPlayed) And (track1.LastPlayed <> "00:00:00") Then
dup = true
End If
Case 3
If cases Then
If track1.Path = track2.Path Then
dup = true
End If
Else
If UCase(track1.Path) = UCase(track2.Path) Then
dup = true
End If
End If
Case 4
If cases Then
If track1.ArtistName = track2.ArtistName Then
dup = true
End If
Else
If UCase(track1.ArtistName) = UCase(track2.ArtistName) Then
dup = true
End If
End If
End Select
End Function
Sub fixList(name,found,dupby,selname,recurse,cases)
Dim list : Set list = SDB.PlaylistByTitle(name)
Dim tracks : Set tracks = list.Tracks
Dim i : i = 0
Dim j : j = 0
If name = selname Then
found = True
End If
If (found) And (tracks.Count > 1) Then
Dim progress : Set progress = SDB.Progress
progress.MaxValue = tracks.Count
progress.Text = "Scanning playlist '"&name&"' for duplicates ("&tracks.Count&" tracks)"
For i = 0 To tracks.Count-2
For j = i+1 To tracks.Count-1
If dup(tracks.Item(i),dupby,tracks.Item(j),cases) Then
Call list.RemoveTrack(tracks.Item(j))
End If
If progress.Terminate Then
Exit For
End If
Next
If progress.Terminate Then
Exit For
End If
progress.Value = i+1
Next
If progress.Terminate Then
Exit Sub
End If
Set progress = Nothing
End If
If (recurse) Or (Not found) Then
Dim children : Set children = list.ChildPlaylists
For i = 0 To children.count-1
Call fixList(children.Item(i).Title,found,dupby,selname,recurse,cases)
If (found) And (Not recurse) Then
Exit For
End If
Next
End If
End Sub
Sub fixNP(dupby,cases)
SDB.Player.Stop
SDB.Player.CurrentSongIndex = 0
Dim tracks : Set tracks = SDB.Player.CurrentSonglist
Dim progress : Set progress = SDB.Progress
Dim m : m = ","
Dim i : i = 0
Dim j : j = 0
If tracks.Count > 1 Then
progress.MaxValue = tracks.Count
progress.Text = "Scanning Now Playing list for duplicates ("&tracks.Count&" tracks)"
For i = 0 To tracks.Count-2
For j = i+1 To tracks.Count-1
If dup(tracks.Item(i),dupby,tracks.Item(j),cases) And (InStr(m,","&j&",") = 0) Then
m = m&j&","
End If
If progress.Terminate Then
Exit For
End If
Next
If progress.Terminate Then
Exit For
End If
progress.Value = i+1
Next
End If
If Not (progress.Terminate) Then
If Len(m) > 1 Then
m = Mid(m,2,Len(m)-2)
Dim a : a = Split(m,",")
progress.MaxValue = UBound(a)+1
progress.Value = 0
progress.Text = "Removing "&(UBound(a)+1)&" tracks from Now Playing list"
Dim b : b = False
Do
b = True
For i = 0 To UBound(a)-1
If Int(a(i+1)) > Int(a(i)) Then
b = False
m = a(i)
a(i) = a(i+1)
a(i+1) = m
End If
Next
Loop Until b
For i = 0 To UBound(a)
SDB.Player.PlaylistDelete(a(i))
progress.Value = i+1
Next
Else
Call SDB.MessageBox("Now Playing has no duplicates.",mtError,Array(mbOk))
End If
End If
End Sub
Sub FixPlaylistDups
Dim Form : Set Form = SDB.UI.NewForm
Form.Common.SetRect 100, 100, 350, 135
Form.BorderStyle = 3 ' Resizable
Form.FormPosition = 4 ' Screen Center
Form.Caption = "Fix Playlist Dups"
Dim Label1 : Set Label1 = SDB.UI.NewLabel(Form)
Label1.Caption = "Duplicates by:"
Label1.Common.Left = 10
Label1.Common.Top = 14
Dim Drp1 : Set Drp1 = SDB.UI.NewDropdown(Form)
Drp1.Common.Left = Label1.Common.Left + Label1.Common.Width +25
Drp1.Common.Top = Label1.Common.Top -4
Drp1.Common.Width = Form.Common.Width - Drp1.Common.Left -20
Drp1.Common.ControlName = "Drop1"
Drp1.Style = 2
Drp1.AddItem("Track ID")
Drp1.AddItem("Artist & Title")
Drp1.AddItem("Last played")
Drp1.AddItem("File path")
Drp1.AddItem("Artist")
Drp1.ItemIndex = SDB.IniFile.IntValue("Scripts","DupBy")
Drp1.UseScript = Script.ScriptPath
Drp1.OnSelectFunc = "SelDupMode"
Dim Label2 : Set Label2 = SDB.UI.NewLabel(Form)
Label2.Caption = "Playlist name:"
Label2.Common.Left = 10
Label2.Common.Top = 39
Dim Edt1 : Set Edt1 = SDB.UI.NewDropdown(Form)
Edt1.Common.Left = Drp1.Common.Left
Edt1.Common.Top = Label2.Common.Top -4
Edt1.Common.Width = Form.Common.Width - Edt1.Common.Left -20
Edt1.Common.ControlName = "Edit1"
Edt1.Style = 2
Edt1.AddItem("[Now Playing]")
Edt1.AddItem("[All]")
Call AddPlaylists(Edt1)
Edt1.ItemIndex = 0
Edt1.UseScript = Script.ScriptPath
Edt1.OnSelectFunc = "SelPlaylist"
Dim Chk2 : Set Chk2 = SDB.UI.NewCheckbox(Form)
Chk2.Common.Left = 10
Chk2.Common.Top = Label2.Common.Top +20
Chk2.Common.Width = Form.Common.Width -25
Chk2.Common.ControlName = "Chck2"
Chk2.Caption = "Case sensitive?"
Chk2.Checked = True
Select Case Drp1.ItemIndex
Case 1
Chk2.Common.Visible = True
Case 3
Chk2.Common.Visible = True
Case 4
Chk2.Common.Visible = True
Case Else
Chk2.Common.Visible = False
End Select
Dim Chk1 : Set Chk1 = SDB.UI.NewCheckbox(Form)
Chk1.Common.Left = 10
Chk1.Common.Top = Chk2.Common.Top +15
Chk1.Common.Width = Form.Common.Width -25
Chk1.Common.ControlName = "Chck1"
Chk1.Caption = "Include children?"
Chk1.Checked = False
Chk1.Common.Visible = False
Dim BtnOk : Set BtnOk = SDB.UI.NewButton(Form)
BtnOk.Caption = "&Ok"
BtnOk.Common.Top = Edt1.Common.Top + Edt1.Common.Height +10
BtnOk.Default = True
BtnOk.ModalResult = 1
Dim BtnCancel : Set BtnCancel = SDB.UI.NewButton(Form)
BtnCancel.Caption = "&Cancel"
BtnCancel.Common.Left = Form.Common.Width - BtnCancel.Common.Width -20
BtnOK.Common.Left = BtnCancel.Common.Left - BtnOK.Common.Width -10
BtnCancel.Common.Top = BtnOK.Common.Top
BtnCancel.Cancel = True
BtnCancel.ModalResult = 2
If Form.ShowModal = 1 Then
Dim dupby : dupby = Drp1.ItemIndex
Dim cases : cases = Chk2.Checked
SDB.IniFile.IntValue("Scripts","DupBy") = dupby
If Edt1.Text = "[Now Playing]" Then
Call fixNP(dupby,cases)
Else
Dim found : found = False
Dim sname : sname = Edt1.Text
Dim recur : recur = Chk1.Checked
If Edt1.Text = "[All]" Then
sname = ""
recur = True
End If
Call fixList("",found,dupby,sname,recur,cases)
If Not found Then
Call SDB.MessageBox("Playlist entered was not found!",mtError,Array(mbOk))
End If
End If
End If
End Sub
Sub AddPlaylists(drp)
'find names
Dim dic : Set dic = CreateObject("Scripting.Dictionary")
Call AddPlaylistsRec(dic,"",True)
'sort them
Dim i : i = 0
Dim a : a = dic.Keys
Dim b : b = True
Dim m : m = ""
While b
b = False
For i = 0 To UBound(a)-1
If a(i+1) < a(i) Then
b = True
m = a(i)
a(i) = a(i+1)
a(i+1) = m
End If
Next
WEnd
'add to list
For i = 0 To UBound(a)
drp.AddItem(a(i))
Next
End Sub
Sub AddPlaylistsRec(dic,nam,aut)
Dim i : i = 0
Dim list : Set list = SDB.PlaylistByTitle(nam)
If Not (list Is Nothing) Then
If (aut = False) And (Len(nam) > 0) Then
dic.Item(nam) = nam
End If
Dim kids : Set kids = list.ChildPlaylists
For i = 0 To kids.Count-1
Dim itm : Set itm = kids.Item(i)
Call AddPlaylistsRec(dic,itm.Title,itm.isAutoPlaylist)
Next
End If
End Sub
Sub SelPlaylist(edt)
Dim chk : Set chk = edt.Common.TopParent.Common.ChildControl("Chck1")
chk.Checked = False
Select Case edt.Text
Case "[Now Playing]"
chk.Common.Visible = False
Case "[All]"
chk.Common.Visible = False
Case Else
If SDB.PlaylistByTitle(edt.Text).ChildPlaylists.Count = 0 Then
chk.Common.Visible = False
Else
chk.Common.Visible = True
End If
End Select
End Sub
Sub SelDupMode(drp)
Dim chk : Set chk = drp.Common.TopParent.Common.ChildControl("Chck2")
Select Case drp.ItemIndex
Case 1
chk.Common.Visible = True
Case 3
chk.Common.Visible = True
Case 4
chk.Common.Visible = True
Case Else
chk.Common.Visible = False
End Select
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("FixPlaylistDups","Filename") = "FixPlaylistDups.vbs"
inif.StringValue("FixPlaylistDups","Procname") = "FixPlaylistDups"
inif.StringValue("FixPlaylistDups","Order") = "9"
inif.StringValue("FixPlaylistDups","DisplayName") = "Fix Playlist Dups"
inif.StringValue("FixPlaylistDups","Description") = "Fix Playlist Duplications"
inif.StringValue("FixPlaylistDups","Language") = "VBScript"
inif.StringValue("FixPlaylistDups","ScriptType") = "0"
SDB.RefreshScriptItems
End If
End Sub