' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
' This file can be replaced  in one of the future versions,
' so please if you want to modify it, make  a copy, do your
' modifications  in that copy and  change Scripts.ini  file 
' appropriately. 
' If you do not do this, you will lose all  your changes in
' this script when you install a new version of MediaMonkey
' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Option Explicit     ' report undefined variables, ...

' SDB variable is connected to MediaMonkey application object

' Recursively process all playlists
Sub ReadPlaylists( playlists, plst, prefix)
  Dim items
  Set items = plst.ChildPlaylists

  If prefix<>"" Then
    prefix = prefix & " - "
  End If

  Dim i, newplst, title
  For i=0 To items.Count-1
    Set newplst = items.Item(i)
    title = prefix & newplst.Title
    If Not playlists.exists(title) Then
      playlists.Add title, newplst
    End If
    ReadPlaylists playlists, newplst, title
  Next
End Sub

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

  ' Let user select the output path
  Dim path
  path = iniF.StringValue( "Scripts", "LastExportM3UsDir")

  path = SDB.SelectFolder( path, SDB.Localize( "Select where to export all M3U files."))

  If path="" Then
    Exit Sub
  End If

  If Right( path, 1)<>"\" Then
    path = path & "\"
  End If

  ' Write selected directory to the ini file
  iniF.StringValue( "Scripts", "LastExportM3UsDir") = path
  Set iniF = Nothing

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

  Dim playlists
  Set playlists = CreateObject("Scripting.Dictionary")

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

  ' Prepare a list of all playlists
  ReadPlaylists playlists, SDB.PlaylistByTitle( ""), ""

  ' Go through the list and export each playlist
  Dim i, iTrck, plst, fout, plsts, titles, title, tracks, trck, ln, tlen, art, tit
  plsts = playlists.Items
  titles = playlists.Keys
  Progress.MaxValue = playlists.count
  For i=0 To playlists.Count-1
    Set plst = plsts(i)
    Set tracks = plst.Tracks
    title = Titles(i)
    Progress.Text = ExpText & " (" & title & ")"
    If tracks.Count>0 Then
      Set fout = fso.CreateTextFile( path & fso.CorrectFilename(title) & ".m3u", True)
      fout.WriteLine "#EXTM3U"
      For iTrck=0 To tracks.Count-1
        Set trck = tracks.Item(iTrck)
        ln = "#EXTINF:"
        tlen = trck.SongLength
        If tlen>0 Then
          ln = ln & tlen \ 1000 & ","
        Else
          ln = ln & "-1,"
        End If
        art = trck.ArtistName
        tit = trck.Title
        If art<>"" Then
          If tit<>"" Then
            ln = ln & art & " - " & tit
          Else
            ln = ln & art
          End If
        Else
          If tit<>"" then
            ln = ln & tit
          End If
        End If
        fout.WriteLine ln 
        fout.WriteLine trck.Path
      Next
      fout.Close
    End If
    Progress.Value = i+1
  Next
End Sub