Export MediaMonkey Tracks to Simple Song List

I wrote my first MediaMonkey script today. It exports the selected songs or playlists to a text file, using the information and format I prefer for making CD covers for my mixes: [Track]. [Artist] - [Title]

Here's the block that goes in the scripts.ini file, which lives in C:\Program Files (x86)\MediaMonkey\Scripts:

DisplayName=Export Songlist
Description=Export tracks to textfile for CD cover

And here's the actual script, which is saved as a .vbs file in that same scripts folder:

' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
' Exports the currently selected list to a text file in
' a format suitable for insertion into a custom-designed
' CD cover.
' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Option Explicit ' report undefined variables, ...

Dim list ' list of songs to be exported
Dim res ' results of dialogs calls
Dim fullfile ' fully specified output file name
Dim fso ' FileSystemObject

' function for quoting strings
Function QStr( astr)
QStr = chr(34) & astr & chr(34)
End Function

' function for quoting strings converted to plain ASCII
Function QAStr( astr)
QAStr = chr(34) & SDB.toASCII(Replace(astr,"""","""""")) & chr(34)
End Function

Sub InitExport( ext, filter, iniDirValue)
fullfile = ""

' Get a list of songs to be exported
Set list = SDB.CurrentSongList

If list.count=0 Then
res = SDB.MessageBox( SDB.Localize("Select files to be exported, please."), mtError, Array(mbOk))
Exit Sub
End If

' Open inifile and get last used directory
Dim iniF
Set iniF = SDB.IniFile

' Create common dialog and ask where to save the file
Dim dlg
Set dlg = SDB.CommonDialog
dlg.Flags=cdlOFNOverwritePrompt + cdlOFNHideReadOnly + cdlOFNNoChangeDir
dlg.InitDir = iniF.StringValue( "Scripts", iniDirValue)

if Not dlg.Ok Then
Exit Sub ' if cancel was pressed, exit
End If

' Get the selected filename
fullfile = dlg.FileName

' Connect to the FileSystemObject
Set fso = SDB.Tools.FileSystem

' Write selected directory to the ini file
iniF.StringValue( "Scripts", iniDirValue) = fullfile
End Sub

Sub FinishExport( ok)
On Error Resume Next

' remove the output file if terminated
if not Ok then
fso.DeleteFile( fullfile)
end if
End Sub

Sub ExportSonglist
' initialize export
Call InitExport (".txt", SDB.Localize("TXT (*.txt)|*.txt|All files (*.*)|*.*"), _
if fullfile="" then
Exit Sub
end if

' Create the output file
Dim fout
Set fout = fso.CreateTextFile( fullfile, True)

' Use progress to notify user about the current action
Dim Progress
Set Progress = SDB.Progress
Progress.Text = SDB.Localize("Exporting...")

' Iterate through the list and export all songs
Progress.MaxValue = list.count
Dim i, itm
for i=0 to list.count-1
Set itm = list.Item(i)
fout.WriteLine Join( Array( CStr(i + 1), ". ", itm.ArtistName, " - ", itm.title ), "")
Progress.Value = i+1
if Progress.Terminate then
Exit For
end if

' Close the output file and finish

' Was it successfull?
Dim ok
if Progress.Terminate then
ok = False
ok = True
end if

' hide progress
Set Progress = Nothing

Call FinishExport( ok)
End Sub

comments powered by Disqus