I'm trying to modify this script to work for multiple cases.
First I didn't want to have split playlist so I got rid of that part.
Next I wanted it to work in the case of selecting some files and folders, but I'm at a loss for how to process the selected directories?
- Nothing selected (use sourcetab and process it)
- Something Selected
- Process Each Folder, Get all files
- Make a M3U with all the selected files
The trouble I'm having is the FolderEnum
vs Enumerator
syntax. One uses atEnd()
and another complete
How Can I make it work for all the situations described?
See my code as of now:
function isAudio(extension) {
switch (extension.toLowerCase()) {
case '.mp3':
case '.wav':
case '.ogg':
case '.flac':
case '.wma':
case '.m4v':
case '.m4a':
case '.m4b':
return true;
}
return false;
}
function collectAudioFiles(objEnum) {
var objFiles = new Array();
var objT;
var i = 0;
while (!objEnum.atEnd()) {
objT = objEnum.item();
if (isAudio(objT.ext) == true) {
objFiles.push(objT);
}
++i;
objEnum.moveNext();
}
objFiles.sort();
return objFiles;
}
function genM3U(objEnum, bAbsolute) {
if (typeof bAbsolute === 'undefined') {
bAbsolute = true;
}
// Original code here
if (objEnum.atEnd()) {
DOpus.Output("No Items Found");
return;
}
// build an array of the files so we can sort it
var objFiles = collectAudioFiles(objEnum);
//DOpus.Output("Length: "+objFiles.length);
//DOpus.Output("Files: "+objFiles);
//clickData.func.command.ClearFiles();
if (objFiles.length < 1) {
DOpus.Output("No Audio Files Found");
return;
}
var audiobookFolderName = objFiles[0].path.filepart+".m3u";
var audiobookFolderPath = objFiles[0].path;
var m3uName = audiobookFolderPath + '\\' + audiobookFolderName;
var fso, f1;
var ForWriting = 2;
//var Dopus = DOpus.CreateCommand;
fso = new ActiveXObject("Scripting.FileSystemObject");
var i = 0;
if (typeof m3uName !== "undefined") {
DOpus.Output("Creating M3U File with "+objFiles.length+" entries.");
f1 = fso.CreateTextFile(m3uName, ForWriting);
f1.Write("#EXTM3U"+'\r')
}
for (i; i < objFiles.length; i++) {
if (objFiles[i] != null) {
if (bAbsolute) {
f1.Write((objFiles[i].RealPath) + '\r');
}
else {
f1.Write((objFiles[i].name) + '\r');
}
}
}
f1.close();
delete objFiles;
DOpus.Output("Created M3U File:"+m3uName);
delete objEnum;
}
function filesFromDir(path) {
}
function OnClick(clickData) {
var objCmd = clickData.Func.Command;
// How to check if SHIFT was pressed? and set the absolute value to false
// mine
if (clickData.func.sourcetab.selected.count == 0) {
DOpus.Output("Nothing Selected, Processing current directory");
genM3U(new Enumerator(objCmd.sourcetab.files)); // call original code to create m3u for currently selected tab
}
else {
genM3U(new Enumerator(objCmd.sourcetab.selected_files));
//genM3U(new Enumerator(objCmd.sourcetab.selected_dirs)); //This doesn't work
for (var eSel = new Enumerator(objCmd.sourcetab.selected_dirs); !eSel.atEnd(); eSel.moveNext()) {
// collect files in a collection
var objFiles = []
var objT;
var eFolEnum = DOpus.FSUtil.ReadDir(eSel.item().RealPath)
while(!eFolEnum.complete) {
objT = eFolEnum.Next()
if (!objT.is_dir) {
objFiles.push(objT);
}
}
eFolEnum.Close();
genM3U(new Enumerator(objFiles));
}
}
}
Actually I was able to get it to work by doing something dumb. Loading all the files into memory and then pushing it onto the enumerator.
Sigh. Any feedback is welcome, feels too hackish right now, but will work for all my cases right now.
Download the button below.
Create M3U(s).dcf (6.7 KB)