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)

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

”Written By: Matthew Maxson (
”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
”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
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)
”Used with permission


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)
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
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 TheReturn

set FSO = CreateObject(“Scripting.FileSystemObject”)

if FSO.FileExists(ThePathToWPLFiles & ThePlaylistName) then
TheReturn = True
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 file

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

file.WriteLine(“” & vbCRLF)



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 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)



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

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.

Its almost time!

It's been quite a while since I've taken time to write. Pregnancy has been pretty fun and largely uneventful.  Everything has gone very well.  I feel good and Dr. Royal is very pleased with how we've done.  Matt and I have taken the labor/delivery classes and toured the hospital.  We're just waiting now…resting, reading baby books, making sure everything at home is ready, etc.   

She's not much of a kicker…her favorite activity is to stretch and push on mommy 🙂 Although, she's doing that less often these days as she has pretty much run out of space. While I am obviously in later pregnancy, I haven't grown out all that much, so she's really got to be feeling cramped! I'm not miserable or itching to be done with pregnancy, but I am definitely ready to hold her and share her with Matt (and the rest of our family and friends)!! We have been so blessed through this whole experience, how can we not recognize how fearfully and WONDERFULLY made by God we are??!!

As we wait for her to make her grand entrance, rest assured that we will post all the pertinent details and pictures when the time comes!!

Weight — 14 Jan – 20 Jan

Here's some stats on my weight:

1 – 6 Jan = 248lbs (single measurement)
7 – 13 Jan = 241.75lbs (avg over 4 measurements)
14 – 20 Jan = 240.8lbs (avg over 5 measurements)

So, the trend is down.  My goal is 220lbs by June 1.

Weight — 15 Jan 2007

I currently weigh 243.  In the next 4 months, I need to drop 20-25 lbs.  This is safe (it works out to a tad over 1 lb per week).  As an incentive to me, I plan to post my weekly average weights here.

Thanks for listening.

First Bike Ride of 2007

Well, I rode my bike to work for the first time in 2007 this morning.  I couldn't find my bike computer so I can't post stats; however, I felt as if I hadn't been on the bike in 3 months (and I hadn't…I hadn't ridden since The Missions Century in October!).  Although I don't like the weather currently (it is supposed to be 65-70 today), it sure does help the riding!

Sandisk Sansa mp3 Player

For Christmas, I recevied a SanDisk Sansa e270 (I specifically got the e270r which has a little different firmware and it only available at Best Buy as it is configured to work with the Best Buy/Rhaposdy music service. Which, by the way, I received a 2 month trial to). I hadn't done any research, but I knew I didn't want a ipod (I had several reasons for this…the biggest was that Apple is too big andhas too much of an attitude). I didn't know it, but the Sansa is the #2 mp3 player on the market (right behind the ipod). It's biggest plus is a 20 hour capacity user replaceable battery!

It has taken me a while to figure some things out (like how I can subscrbe to podcasts, have them downloaded, and transferred to the player automatically). At this point, I have an ok solution and I thought I would write about it so it was available to everyone else. First though, I'll talk about some thing that didn't work.

Best Buy Music Store
: As I mentioned above, the e270r came with a 2 month trial to the Best Buy Music Store (you pay $15/month and can download all the music you want to your player). This was my first stop. I signed up and started downloading. It was nice…I could search for music I didn't have CDs for, download it, and listen. I could also import my mp3s and transfer them to my player. I could make play lists, etc…. The only drawbacks were: the music you download is DRM tied to the player and service…once you quit paying the music stops working, there is no way to build non-music store playlists automatically (like for a podcast), and there is no way to subscribe to podcasts.

ITunes: I thought this would only sync if I had an ipod, but I thought I would give it a shot. I could downlad podcasts easily enough, but it wouldn't sync to my player. I found an add-on called idleTunes that claims it would Sync my ITunes playlists to other players. ITunes downloaded the podcasts good enough; however, I had no way to get the music onto my player. idleTunes helped, but it wouldn't create playlists in the playlists menu of my player (this means I was limited to the Sansa relying on id3 tags which don't always exist in podcast files). It also wasn't automatic. I had to manually tell idleTunes to copy the playlists.

Media Monkey: This app was highly rated on theanythingbutipod forums. I downloaded it, but didn't get very far. In the 5 minutes I spent playing with it, I couldn't get it to work. Therefore, I decided not to use it (I expect to get programs up and working fairly fast).

saCaster: I was searching UseNet and found reference to saCaster. I never even installed this because it wasn't free software (free in any sense…free pizza or free speech). Sure, there was a "shareware" version, but it was only a 15 day trial. I knew there had to be free alternatives out there, so I kept looking.

Windows Media Player
: When put into PlaysForSure mode, WMP would recognize the player and sync files. I could also get playlists on my sansa (although, I have to sync twice for some reason). The only problem was WMP is a Microsoft app (I don't like Microsoft). It also doesn't have podcast support (if it did, I would probably be ok with this). It would autosync to my player. You can set up auto playlists, but you can only do that based on metadata in the file…you can't say "pull all the files from this directory." That is an aggrivation as some/most podcasts don't have id3 information filled out correctly.

Juice/iPodder: Juice (formerly iPodder…they changed names after a fight with Apple). This app provided a nice way to sbscribe to podcasts and will autodownload them. It's also free software (free as in pizza and free as in speech. It's distributed under the GPL) But, sigh, there is no way to coy the files to my Sansa. Even if it did, I doubt it would generate playlists.

After trying that, I was out of ideas. It had been 2 weeks since Christmas and I wasn't real happy with what I had figured out so far. Something shouldn't be this hard. Well, after trying Juice, I opened WMP up, and noticed playlists for the podcasts I had subscribed to. Interesting. So, I added a few new podcasts to Juice, synched them, opened WMP and they showed up. Once I set them up to sync, everything ended up on my ipod corectly.

So, where I am right now…I have Juice downloading my podcasts. WMP is set up to monitor that directory for new items to add to my library. Juice manages the playlists for me (when it downloads new stuff, it updates a WMP playlist). I can then sync things over to my sansa. I'll give this a few weeks then I'll update again after spending some additional time with this setup.

[Update 6 Jan 2007] Well, it turns out that my WMP playlists aren't being updated when Juice downloads new files. I don't know what the problem is, but I am looking into it. When I find out the problem, I'll post it here (with hopefully a fix too)

[Update 7 Jan 2007] I used sysinternals filemon tool to see what Juice was doing. It was checking some database in the WMP folder. This got me to wonder if it was checking to see if WMP knew of the files or not before adding them to my playlist. So, I fiddled with some settings in WMP and had it NOT monitor the folders where I download my podcasts to. For the time being, that makes a difference.

[Update 27 Jan 2007] Refer to this post for a script I wrote that solved my Juice podcast and playlist problems.