My script will split an audiobook every Nth file, creating a new m3u playlist until all audio files are counted. You can choose to split the audiobook every 2 to 10 files from the dialog prompt. The m3u file name is based on the current folder name with a _CD number at the end. If you have any album art images in the audiobook folder they are ignored. Only audio files will be added to the playlist (mp3, wav, ogg, m4v, flac,wma).
Example:
I have 74 mp3 files in the audiobookTitleHere folder. Lets say one mp3 file is 5 minutes in length. If I wanted to listen for 50 minutes, I'd create a playlist every 10 files. You can lower this number to listen for less time.
[imgur link is now broken]
Install:
1. Right click any toolbar
2. Click Customize...
3. Copy and paste directly to your toolbar
<?xml version="1.0"?>
<button backcol="none" display="label" label_pos="right" textcol="none">
<label>Audiobook M3U Generator</label>
<tip>Splits the audiobook every nth file (2-10)</tip>
<icon1>#newcommand</icon1>
<function type="script">
<instruction>@script jscript</instruction>
<instruction>function zeroPad(num)</instruction>
<instruction>{</instruction>
<instruction> var numZeropad = num + '';</instruction>
<instruction> while(numZeropad.length < 2){</instruction>
<instruction> numZeropad = "0" + numZeropad;</instruction>
<instruction>}</instruction>
<instruction>return numZeropad;</instruction>
<instruction>}</instruction>
<instruction>function numberCDS(CD){</instruction>
<instruction> var getCDNumber = zeroPad(CD);</instruction>
<instruction>return getCDNumber;</instruction>
<instruction>}</instruction>
<instruction>function isAudio(extension)</instruction>
<instruction>{</instruction>
<instruction> switch (extension.toLowerCase()){</instruction>
<instruction> case '.mp3':</instruction>
<instruction> case '.wav':</instruction>
<instruction> case '.ogg':</instruction>
<instruction> case '.m4v':</instruction>
<instruction> case '.flac':</instruction>
<instruction> case '.wma':</instruction>
<instruction> return true;</instruction>
<instruction> }</instruction>
<instruction> return false;</instruction>
<instruction>}</instruction>
<instruction>function OnClick(ClickData)</instruction>
<instruction>{</instruction>
<instruction> var objCmd = ClickData.Func.Command;</instruction>
<instruction> var allFiles = objCmd.sourcetab.files;</instruction>
<instruction> var objEnum = new Enumerator(allFiles);</instruction>
<instruction> if (!objEnum.atEnd())</instruction>
<instruction> {</instruction>
<instruction> // build an array of the files so we can sort it</instruction>
<instruction> var objFiles = new Array();</instruction>
<instruction> i = 0;</instruction>
<instruction> while (!objEnum.atEnd())</instruction>
<instruction> {</instruction>
<instruction> objFiles[i] = objEnum.item();</instruction>
<instruction> if (isAudio(objFiles[i].ext) == false)</instruction>
<instruction> {</instruction>
<instruction> objFiles.splice (i, 1);</instruction>
<instruction> }</instruction>
<instruction> ++i;</instruction>
<instruction> objEnum.moveNext();</instruction>
<instruction> }</instruction>
<instruction> objFiles.sort();</instruction>
<instruction> ClickData.func.command.ClearFiles();</instruction>
<instruction> audiobookFolderName = objCmd.sourcetab.path.filepart;</instruction>
<instruction> var audiobookFolderPath = objCmd.sourcetab.path;</instruction>
<instruction> var fso, f1;</instruction>
<instruction> var ForWriting = 2;</instruction>
<instruction> var totalNumberOfFiles = objCmd.sourcetab.stats.files;</instruction>
<instruction> var Dopus = DOpus.CreateCommand;</instruction>
<instruction> var dlg = Dopus.Dlg;</instruction>
<instruction> dlg.window = DOpus.Listers(0);</instruction>
<instruction> dlg.message = "Split audiobook every nth file:";</instruction>
<instruction> dlg.title = "Audiobook M3U Generator";</instruction>
<instruction> dlg.buttons = "OK|Cancel";</instruction>
<instruction> dlg.choices = DOpus.NewVector("2", "3", "4", "5", "6", "7", "8", "9", "10")</instruction>
<instruction> dlg.selection = 0</instruction>
<instruction> ret = dlg.show;</instruction>
<instruction> if (ret == 0)</instruction>
<instruction> {</instruction>
<instruction> return;</instruction>
<instruction> }</instruction>
<instruction> var chosenOutput = dlg.choices(dlg.selection);</instruction>
<instruction> switch (chosenOutput){</instruction>
<instruction> case '2':</instruction>
<instruction> var chosenNumber = 2;</instruction>
<instruction> break; </instruction>
<instruction> case '3':</instruction>
<instruction> var chosenNumber = 3; </instruction>
<instruction> break;</instruction>
<instruction> case '4':</instruction>
<instruction> var chosenNumber = 4; </instruction>
<instruction> break;</instruction>
<instruction> case '5':</instruction>
<instruction> var chosenNumber = 5;</instruction>
<instruction> break;</instruction>
<instruction> case '6':</instruction>
<instruction> var chosenNumber = 6;</instruction>
<instruction> break;</instruction>
<instruction> case '7':</instruction>
<instruction> var chosenNumber = 7;</instruction>
<instruction> break;</instruction>
<instruction> case '8':</instruction>
<instruction> var chosenNumber = 8;</instruction>
<instruction> break;</instruction>
<instruction> case '9':</instruction>
<instruction> var chosenNumber = 9;</instruction>
<instruction> break;</instruction>
<instruction> case '10':</instruction>
<instruction> var chosenNumber = 10;</instruction>
<instruction> break;</instruction>
<instruction> }</instruction>
<instruction> var x = totalNumberOfFiles / chosenNumber;</instruction>
<instruction> var totalCDs = Math.floor(x);</instruction>
<instruction> if ((totalNumberOfFiles % chosenNumber) !== 0)</instruction>
<instruction> {</instruction>
<instruction> totalCDs = Math.floor(x) + 1;</instruction>
<instruction> totalNumberOfFiles = totalNumberOfFiles + 10;</instruction>
<instruction> }</instruction>
<instruction> var m3uName;</instruction>
<instruction> function createCD(currentCDNumber)</instruction>
<instruction> {</instruction>
<instruction> var getCdNumber = numberCDS(currentCDNumber);</instruction>
<instruction> if (currentCDNumber <= totalCDs)</instruction>
<instruction> {</instruction>
<instruction> m3uName = audiobookFolderName + "_CD" + getCdNumber + ".m3u";</instruction>
<instruction> return m3uName;</instruction>
<instruction> }</instruction>
<instruction> }</instruction>
<instruction> fso = new ActiveXObject("Scripting.FileSystemObject");</instruction>
<instruction> var i = 0;</instruction>
<instruction> var k = 1;</instruction>
<instruction> do {</instruction>
<instruction> for(incrementbyChosen = 0; incrementbyChosen <= totalNumberOfFiles; incrementbyChosen += chosenNumber)</instruction>
<instruction> {</instruction>
<instruction> createCD(k);</instruction>
<instruction> if (k * chosenNumber == incrementbyChosen)</instruction>
<instruction> {</instruction>
<instruction> if (typeof m3uName !== "undefined")</instruction>
<instruction> {</instruction>
<instruction> f1 = fso.CreateTextFile(audiobookFolderPath + "\\" + m3uName, ForWriting);</instruction>
<instruction> }</instruction>
<instruction> for (i; i < objFiles.length && i < incrementbyChosen; i++)</instruction>
<instruction> {</instruction>
<instruction> if (objFiles[i] != null)</instruction>
<instruction> {</instruction>
<instruction> f1.Write((objFiles[i].name) + '\r');</instruction>
<instruction> }</instruction>
<instruction> }</instruction>
<instruction> }</instruction>
<instruction> }</instruction>
<instruction> k++;</instruction>
<instruction> } while (k <= totalCDs);</instruction>
<instruction> {</instruction>
<instruction> delete objFiles;</instruction>
<instruction> }</instruction>
<instruction> delete objEnum;</instruction>
<instruction> }</instruction>
<instruction>}</instruction>
</function>
</button>
This was my very first script using JavaScript. Feedback is welcome.