I have had a look at the code and made some comments and alterations, I haven't tested it though...
[code]
#target photoshop
function main(){
if(!documents.length) return;
var doc = activeDocument;
var dName = decodeURI(activeDocument.name).replace(/\.[^\.]+$/, '');
var oldPath = (activeDocument.path);
var rndrDir = new Folder(oldPath+"/"+"JPEG");
if(!rndrDir.exists) rndrDir.create();
//the /JPEG/ should be in quotes
//Why are you re declaring the output folder?
//rndrDir has been declared as the output folder and created if it did not exist
//var newPath = (activeDocument.path + /JPEG/);
for(var a=0;a<doc.layerSets.length;a++){
activeDocument.activeLayer = activeDocument.layers.getByName(doc.layerSets[a].name);
//get the layers name before the layer is duplicated to its own document.
var lName = doc.layerSets[a].name;
//the following line duplicates the layer to its own document
dupLayers();
//the following line should not be required as only one layer copied
activeDocument.mergeVisibleLayers();
//newPath was incorrect, doc.layerSets[a].name does not exist as this layer is now by itself in its own document.
//var saveFile= File(newPath +"/"+dName+" - "+doc.layerSets[a].name +".jpg");
var saveFile= File(rndrDir +"/"+dName+" - "+ lName +".jpg");
SaveJPEG(saveFile, 12);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
main();
function dupLayers() {
var desc143 = new ActionDescriptor();
var ref73 = new ActionReference();
ref73.putClass( charIDToTypeID('Dcmn') );
desc143.putReference( charIDToTypeID('null'), ref73 );
desc143.putString( charIDToTypeID('Nm '), activeDocument.activeLayer.name );
var ref74 = new ActionReference();
ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc143.putReference( charIDToTypeID('Usng'), ref74 );
executeAction( charIDToTypeID('Mk '), desc143, DialogModes.NO );
};
function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}
[/code]