It allows you to export your autoplaylist criteria (not the songs themselves, nor manual playlists) into a separate database. You can then use this script to import those criteria into your MM database. This can be used for backing up autoplaylists but cannot be used for transferring them from one database to another as the IDs would not match.
As always, installers for this script can be downloaded from my website!
Code: Select all
'
' MediaMonkey Script
'
' NAME: BackupPlaylists 2.2
'
' AUTHOR: trixmoto (http://trixmoto.net)
' DATE : 18/10/2010
'
' Thank you to Steegy for his help.
'
' INSTALL: Copy to Scripts directory and add the following to Scripts.ini
' Don't forget to remove comments (') and set the order appropriately
'
' [BackupPlaylists]
' FileName=BackupPlaylists.vbs
' ProcName=BackupPlaylists
' Order=15
' DisplayName=Backup Playlists
' Description=Backup playlists
' Language=VBScript
' ScriptType=0
'
' FIXES: Removed old MM2 code
' Fixed problems caused by apostrophes
'
Option Explicit
Sub BackupPlaylists
Dim Form : Set Form = SDB.UI.NewForm
Form.Common.SetRect 100, 100, 235, 110
Form.BorderStyle = 3
Form.FormPosition = 4
Form.Caption = "Backup Playlists"
Dim Btn1 : Set Btn1 = SDB.UI.NewButton(Form)
Btn1.Caption = "Export to backup"
Btn1.Common.Top = 10
Btn1.Common.Left = 10
Btn1.Common.Width = 200
Btn1.UseScript = Script.ScriptPath
Btn1.ModalResult = 1
Dim Btn2 : Set Btn2 = SDB.UI.NewButton(Form)
Btn2.Caption = "Import from backup"
Btn2.Common.Top = 40
Btn2.Common.Left = 10
Btn2.Common.Width = 200
Btn2.UseScript = Script.ScriptPath
Btn2.ModalResult = 3
Dim result : result = Form.showModal
If result = 1 Then
Call Export()
ElseIf result = 3 Then
Call Import()
End If
End Sub
Function getdbpath()
getdbpath = SDB.IniFile.StringValue("Scripts","BackupPlaylistsDir")
getdbpath = SDB.SelectFolder(getdbpath, "Select your BackupPlaylists database location:")
If getdbpath = "" Then
Exit Function
End If
If Not (Right(getdbpath,1) = "\") Then
SDB.IniFile.StringValue("Scripts","BackupPlaylistsDir") = getdbpath&"\"
getdbpath = getdbpath&"\BackupPlaylists.DB"
Else
SDB.IniFile.StringValue("Scripts","BackupPlaylistsDir") = getdbpath
getdbpath = getdbpath&"BackupPlaylists.DB"
End If
End Function
Sub Export()
If InstallLiteX() Then
Dim output : output = "Starting export..."&Chr(13)
Dim mmpath : mmpath = SDB.Database.Path
output = output&"MMPath="&mmpath&Chr(13)
If Not (mmpath = "") Then
Dim dbpath : dbpath = getdbpath()
output = output&"DBPath="&dbpath&Chr(13)
If Not (dbpath = "") Then
Dim db : Set db = CreateObject("LiteX.LiteConnection")
If Not (db Is Nothing) Then
db.Open(mmpath)
Dim bp : Set bp = CreateObject("LiteX.LiteConnection")
If Not (bp Is Nothing) Then
bp.Open(dbpath)
output = output&"Backup database created"&Chr(13)
bp.Execute("DROP TABLE IF EXISTS Playlists")
bp.Execute("CREATE TABLE Playlists (IDPlaylist integer, PlaylistName text, ParentPlaylist integer, Comment text, IsAutoPlaylist integer, QueryData text, srcMedia integer, srcPath text, Persistent integer, Synchronize integer, CONSTRAINT Index1 PRIMARY KEY (IDPlaylist))")
bp.Execute("BEGIN TRANSACTION")
output = output&"Playlists table created"&Chr(13)
Dim qy : Set qy = db.Prepare("SELECT * FROM Playlists WHERE (IsAutoPlaylist = 1 OR IDPlaylist IN (SELECT ParentPlaylist FROM Playlists))")
If Not (qy Is Nothing) Then
Dim ns : ns = "IDPlaylist, PlaylistName, ParentPlaylist, Comment, IsAutoPlaylist, QueryData, srcMedia, srcPath, Persistent, Synchronize"
While (Not qy.Step())
Dim ri : Set ri = qy.Rows.Item
Dim vs : vs = FixInt(ri(0))&",'"&FixStr(ri(1))&"',"&FixInt(ri(2))&",'"&FixStr(ri(3))&"',"&FixInt(ri(4))&",'"&FixStr(ri(5))&"',"&FixInt(ri(6))&",'"&FixStr(ri(7))&"',"&FixInt(ri(8))&","&FixInt(ri(9))
bp.Execute("INSERT INTO Playlists ("&ns&") Values ("&vs&")")
output = output&"Playlist exported: "&ri(1)&Chr(13)
WEnd
qy.Close()
End If
bp.Execute("COMMIT TRANSACTION")
bp.Close()
Call SDB.MessageBox(output&"Export complete.", mtInformation, Array(mbOk))
Else
Call SDB.MessageBox(output&"Error: 'LiteX' could not be created.",mtError,Array(mbOk))
End If
db.Close()
Else
Call SDB.MessageBox(output&"Error: 'LiteX' could not be created.",mtError,Array(mbOk))
End If
Else
Call SDB.MessageBox(output&"Error: 'BackupPlaylists.DB' was not selected.",mtError,Array(mbOk))
End If
Else
Call SDB.MessageBox(output&"Error: 'MM.DB' could not be found.",mtError,Array(mbOk))
End If
Else
Call SDB.MessageBox(output&"Error: 'LiteX' could not be installed.",mtError,Array(mbOk))
End If
End Sub
Sub Import()
If InstallLiteX() Then
Dim output : output = "Starting import..."&Chr(13)
Dim dbpath : dbpath = getdbpath()
output = output&"DBPath="&dbpath&Chr(13)
If Not (dbpath = "") Then
Dim mmpath : mmpath = SDB.Database.Path
output = output&"MMPath="&mmpath&Chr(13)
If Not (mmpath = "") Then
Dim bp : Set bp = CreateObject("LiteX.LiteConnection")
If Not (bp Is Nothing) Then
bp.Open(dbpath)
Dim db : Set db = SDB.Database
If Not (db Is Nothing) Then
Dim id : id = FixInt(db.OpenSQL("SELECT Max(IDPlaylist) FROM Playlists").ValueByIndex(0))
Dim ns : ns = "IDPlaylist, PlaylistName, ParentPlaylist, Comment, IsAutoPlaylist, QueryData, srcMedia, srcPath, Persistent, Synchronize"
Dim qy : Set qy = bp.Prepare("SELECT * FROM Playlists WHERE (IsAutoPlaylist = 1 OR IDPlaylist IN (SELECT ParentPlaylist FROM Playlists))")
If Not (qy Is Nothing) Then
While (Not qy.Step())
Dim ri : Set ri = qy.Rows.Item
Dim ps : ps = FixInt(db.OpenSQL("SELECT Count(*) FROM Playlists WHERE PlaylistName='"&FixStr(ri(1))&"'").ValueByIndex(0))
If ps = 0 Then
id = id+1
Dim vs : vs = id&",'"&FixStr(ri(1))&"',"&FixInt(ri(2))&",'"&FixStr(ri(3))&"',"&FixInt(ri(4))&",'"&FixStr(ri(5))&"',"&FixInt(ri(6))&",'"&FixStr(ri(7))&"',"&FixInt(ri(8))&","&FixInt(ri(9))
db.ExecSQL("INSERT INTO Playlists ("&ns&") Values ("&vs&")")
output = output&"Playlist imported: "&ri(1)&Chr(13)
Else
output = output&"Playlist already exists: "&ri(1)&Chr(13)
End If
WEnd
qy.Close()
End If
Call SDB.MessageBox(output&"Import complete.", mtInformation, Array(mbOk))
Else
Call SDB.MessageBox(output&"Error: 'LiteX' could not be created.",mtError,Array(mbOk))
End If
Else
Call SDB.MessageBox(output&"Error: 'LiteX' could not be created.",mtError,Array(mbOk))
End If
Else
Call SDB.MessageBox(output&"Error: 'MM.DB' could not be found.",mtError,Array(mbOk))
End If
Else
Call SDB.MessageBox(output&"Error: 'BackupPlaylists.DB' was not selected.",mtError,Array(mbOk))
End If
Else
Call SDB.MessageBox(output&"Error: 'LiteX' could not be installed.",mtError,Array(mbOk))
End If
End Sub
Function InstallLiteX()
InstallLiteX = False
On Error Resume Next
Dim litex : Set litex = CreateObject("LiteX.LiteConnection")
If Not (Err.Number = 0) Then
Err.Clear
Dim dll : dll = SDB.ApplicationPath&"Scripts\sqlite3u.dll"
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(dll) Then
Exit Function
End If
Dim wsh : Set wsh = CreateObject("WScript.Shell")
Dim res : res = wsh.Run("regsvr32 "&Chr(34)&dll&Chr(34)&" /s",1,1)
End If
On Error GoTo 0
InstallLiteX = True
End Function
Function FixInt(num)
If IsNull(num) Then
FixInt = "0"
Else
If num = "" Then
FixInt = "0"
Else
FixInt = CStr(num)
End If
End If
End Function
Function FixStr(str)
If IsNull(str) Then
FixStr = ""
Else
FixStr = Replace(str,"'","''")
End If
End Function
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("BackupPlaylists","Filename") = "BackupPlaylists.vbs"
inif.StringValue("BackupPlaylists","Procname") = "BackupPlaylists"
inif.StringValue("BackupPlaylists","Order") = "15"
inif.StringValue("BackupPlaylists","DisplayName") = "Backup Playlists"
inif.StringValue("BackupPlaylists","Description") = "Backup Playlists"
inif.StringValue("BackupPlaylists","Language") = "VBScript"
inif.StringValue("BackupPlaylists","ScriptType") = "0"
SDB.RefreshScriptItems
End If
End Sub