Script to Create Playlists from Juice

In this post, I talk about my challenges of finding an application to use with my Sansa and podcasts. Well, I settled on Juice. But, I still had an issue with playlists. Juice allows you to launch a program after a file is downloaded, so I decided to write a script to do what I want. I thought I would post it here just in case anyone else is looking to do the same thing.

Here are the steps to get the script to work with Juice (I’m going to assume you already have downloaded Juice, installed it, and have several podcasts actively downloading.  I have Juice 2.2.  If you have a different version, the instructions may be a bit different.):

  1. Download the script (right-click this link and choose save target as.  Remember where you save the file)
  2. Launch Juice
  3. Go to File –> Preferences
  4. Go to the Advanced Tab
  5. Check the “Run this command after each download” checkbox
  6. In the text box, enter the following text (making the correct replacements, of course):  BuildPlaylist.vbs “” “%n” “%f”
  7. PathToVBS is the location you saved the vbs to when you downloaded it.
  8. PathToPlaylistFIles is probably something like c:Documents and SettingsMy DocumentsMy MusicMy Playlists
  9. You want to enclose each item in ” (those are double quote marks).  That way, Windows won’t barf on spaces
  10. Click the Save button

That’s it.  If you need some help, just let me know.  E-mail me at matt (at) techs4esus.com.

The script is below (or click here to download it):

””””””””””””””””””””””””
”Written By: Matthew Maxson (matt@techs4jesus.com)
”Date: 1 Jan 2007
”Copyright (c) Matthew Maxson
”This program is free software; you can redistribute it and/or modify
”it under the terms of the GNU General Public License as published by
”the Free Software Foundation; either version 2 of the License, or
”(at your option) any later version.

”This program is distributed in the hope that it will be useful,
”but WITHOUT ANY WARRANTY; without even the implied warranty of
”MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
”GNU General Public License for more details.

”You should have received a copy of the GNU General Public License
”along with this program; if not, write to the Free Software
”Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

”To view the GPL, visit http://www.gnu.org/licenses/gpl.txt
””””””””””””””””””””””””
Option Explicit

Dim args
Dim num
Dim i
Dim fs
Dim fname
Dim WPLPath
Dim PlaylistName
Dim PathToAdd
Dim objTextFile
Dim objFSO

Function SortedFiles(objFolder, strFilePattern, strSortedBy, blnReverse)
‘From
”http://www.fpschultze.de/smartfaq+faq.faqid+124+PHPSESSID+8d9ee6d45fbecaf8b122c956a124ea58.htm
”Used with permission

Const TEMP_FOLDER = 2
Const WINDOW_STYLE_HIDDEN = 0

Dim objWS, objFS, objFiles, objFile
Dim strCmdLine, strTmpFile
Dim tmpArray
Dim i

SortedFiles = Array()
Set objFS = WScript.CreateObject(“Scripting.FileSystemObject”)
Set objWS = WScript.CreateObject(“WScript.Shell”)
strTmpFile = objFS.GetSpecialFolder(TEMP_FOLDER) & “” & objFS.GetTempName
strCmdLine = “%ComSpec% /C DIR ” & Chr(34) & objFolder.Path
If Right(strCmdLine, 1) “” Then strCmdLine = strCmdLine & “”
strCmdLine = strCmdLine & strFilePattern & Chr(34) & ” /B /A-D /O”
If blnReverse Then strCmdLine = strCmdLine & “-“
Select Case LCase(strSortedBy)
Case “name” strCmdLine = strCmdLine & “N”
Case “size” strCmdLine = strCmdLine & “S”
Case “ext” strCmdLine = strCmdLine & “E”
Case “datetime” strCmdLine = strCmdLine & “D”
Case Else Exit Function
End Select
strCmdLine = strCmdLine & ” > ” & strTmpFile
objWS.Run strCmdLine, WINDOW_STYLE_HIDDEN, True
If objFS.FileExists(strTmpFile) Then
If objFS.GetFile(strTmpFile).Size > 0 Then
tmpArray = Split(objFS.OpenTextFile(strTmpFile).ReadAll, vbNewLine)
objFS.DeleteFile(strTmpFile)
ReDim Preserve tmpArray(UBound(tmpArray) – 1)
Set objFiles = objFolder.Files
For i = 0 to UBound(tmpArray)
Set objFile = objFiles((tmpArray(i)))
Set tmpArray(i) = objFile
Next
SortedFiles = tmpArray
End If
End If

End Function

function DoesPlaylistExist(ThePathToWPLFiles, ThePlaylistName)
”we need to figure out if the playlist already exists or not
Dim FSO
Dim TheReturn

set FSO = CreateObject(“Scripting.FileSystemObject”)

if FSO.FileExists(ThePathToWPLFiles & ThePlaylistName) then
TheReturn = True
else
TheReturn = False
end if

set FSO = nothing
DoesPlaylistExist = TheReturn

end function

function CreatePlaylistHeader(ThePlaylistTitle)
”helper function to create the header
Dim TheReturn

TheReturn = “” & vbCRLF
TheReturn = TheReturn & “” & vbCRLF
TheReturn = TheReturn & “” & vbCRLF
TheReturn = TheReturn & “” & vbCRLF
TheReturn = TheReturn & “” & vbCRLF
” & Replace(Replace(ThePlaylistTitle, “.wpl”, “”), “_”, ” “) & “_SCRIPT TheReturn = TheReturn & “” & vbCRLF
TheReturn = TheReturn & “” & vbCRLF
TheReturn = TheReturn & “” & vbCRLF
TheReturn = TheReturn & “” & vbCRLF

CreatePlaylistHeader = TheReturn
end function

function CreatePlaylistFooter()
”helper function to create the footer of the playlist
Dim TheReturn

TheReturn= “” & vbCRLF
TheReturn= TheReturn & “” & vbCRLF
TheReturn= TheReturn & “” & vbCRLF

CreatePlaylistFooter = TheReturn
end function

sub MakeNewPlaylist(ThePathToWPLFiles, ThePlaylistName, TheFileToAdd)
”we’ll use this to make a new playlist
Dim FSO
Dim file

set FSO = CreateObject(“Scripting.FileSystemObject”)
set file = FSO.CreateTextFile(ThePathToWPLFiles & ThePlaylistName,true)
file.WriteLine(CreatePlaylistHeader(Replace(Replace(ThePlaylistName, “.wpl”, “”), “_”, ” “)))

file.WriteLine(“” & vbCRLF)

file.WriteLine(CreatePlaylistFooter())

file.Close

set file = nothing
set FSO = nothing
end sub

function GetDirectory(TheFileToAdd)
”use this function to split the path/file passed in. This will allow us to include other
”files in the same directory
Dim TheReturn

TheReturn = Left(TheFileToAdd, InStrRev(TheFileToAdd, “”))

GetDirectory = TheReturn
end function

sub EditExistingPlaylist(ThePathToWPLFiles, ThePlaylistName, TheFileToAdd)
”if we need to edit an existing playlist, we’ll use this file
Dim DirToCheck
Dim DirFileList
Dim Folder
Dim FSO
Dim file
Dim Filenamestr
Dim PlaylistFilename

DirToCheck = GetDirectory(TheFileToAdd)

set FSO = CreateObject(“Scripting.FileSystemObject”)
set Folder = FSO.GetFolder(DirToCheck)
DirFileList = SortedFiles(Folder, “*.mp3”, “datetime”, false)

set PlaylistFilename = FSO.CreateTextFile(ThePathToWPLFiles & ThePlaylistName,true)
PlaylistFilename.WriteLine(CreatePlaylistHeader(Replace(Replace(ThePlaylistName, “.wpl”, “”), “_”, ” “)))

for each file in DirFileList
PlaylistFilename.WriteLine(“” & vbCRLF)
next

PlaylistFilename.WriteLine(CreatePlaylistFooter())

PlaylistFilename.Close

set PlaylistFilename = nothing
set FSO = nothing
end sub

‘ OpenTextFile Method needs a Const value
‘ ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8

set args = WScript.Arguments
num = args.Count

if num = 0 then
WScript.Echo “Usage: BuildPlaylist.vbs “
WScript.Quit 1
end if

WPLPath = args.Item(0)

if Right(WPLPath, 1) “” then
WPLPath = WPLPath & “”
end if

PlaylistName = Replace(Replace(Replace(args.Item(1), “:”, “”), “.”, “”), ” “, “_”) & “.wpl”
PathToAdd = args.Item(2)

if DoesPlaylistExist(WPLPath, PlaylistName) then
EditExistingPlaylist WPLPath, PlaylistName, PathToAdd
else

MakeNewPlaylist WPLPath, PlaylistName, PathToAdd
end if

[Update 2012-12-19 06:59:42] In getting ready to migrate my website, I found some broken links.  Fixed the link to the script.

Leave a Reply

Your email address will not be published. Required fields are marked *