function main(){
if(activeDocument.layerSets.length != 5) {
alert("You do not have five layersets!");
return;
}
outputFolder = Folder.selectDialog("Please select the output folder");
if(outputFolder == null) return;
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Parts0 =[];
var Parts1 =[];
var Parts2 =[];
var Parts3 =[];
var Parts4 =[];
for(var a =0 ;a<activeDocument.layerSets[0].layers.length;a++){
activeDocument.activeLayer= activeDocument.layerSets[0].layers[a];
Parts0.push(getActiveLayerIndex());
}
for(var a =0 ;a<activeDocument.layerSets[1].layers.length;a++){
activeDocument.activeLayer= activeDocument.layerSets[1].layers[a];
Parts1.push(getActiveLayerIndex());
}
for(var a =0 ;a<activeDocument.layerSets[2].layers.length;a++){
activeDocument.activeLayer= activeDocument.layerSets[2].layers[a];
Parts2.push(getActiveLayerIndex());
}
for(var a =0 ;a<activeDocument.layerSets[3].layers.length;a++){
activeDocument.activeLayer= activeDocument.layerSets[3].layers[a];
Parts3.push(getActiveLayerIndex());
}
for(var a =0 ;a<activeDocument.layerSets[4].layers.length;a++){
activeDocument.activeLayer= activeDocument.layerSets[4].layers[a];
Parts4.push(getActiveLayerIndex());
}
var layerSel=[];
for(var a in Parts0){
var p0 = Parts0[a];
for(var b in Parts1){
var p1 = Parts1[b];
for(var c in Parts2){
var p2 = Parts2[c];
for(var d in Parts3){
var p3 = Parts3[d];
for(var e in Parts4){
var p4 = Parts4[e];
layerSel.push([[p0],[p1],[p2],[p3],[p4]]);
}
}
}
}
}
for(var z in layerSel){
makeActiveByIndex( Number(layerSel[z][0]));
makeActiveByIndex( Number(layerSel[z][1]),true);
makeActiveByIndex( Number(layerSel[z][2]),true);
makeActiveByIndex( Number(layerSel[z][3]),true);
makeActiveByIndex( Number(layerSel[z][4]),true);
dupLayers("Temp");
var saveFile = new File(outputFolder + "/" + Name +"-" + zeroPad((z+1), 4) +".jpg");
SaveForWeb(saveFile,80);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
main();
function getActiveLayerIndex() {
var ref = new ActionReference();
ref.putProperty( 1349677170 , 1232366921 );
ref.putEnumerated( 1283027488, 1332896878, 1416783732 );
var res = executeActionGet(ref).getInteger( 1232366921 ) - Number( hasBackground() );
return res;
};
function hasBackground(){
var res = undefined;
try{
var ref = new ActionReference();
ref.putProperty( 1349677170 , 1315774496);
ref.putIndex( 1283027488, 0 );
executeActionGet(ref).getString(1315774496 );;
res = true;
}catch(e){ res = false}
return res;
};
function makeActiveByIndex( idx, add ){
add == undefined ? add = false : add = true;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( "Lyr " ), idx)
desc.putReference( charIDToTypeID( "null" ), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};
function dupLayers(NewDocName) {
var desc143 = new ActionDescriptor();
var ref73 = new ActionReference();
ref73.putClass( charIDToTypeID('Dcmn') );
desc143.putReference( charIDToTypeID('null'), ref73 );
desc143.putString( charIDToTypeID('Nm '), NewDocName );
var ref74 = new ActionReference();
ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc143.putReference( charIDToTypeID('Usng'), ref74 );
executeAction( charIDToTypeID('Mk '), desc143, DialogModes.NO );
};
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality; //0-100
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
function zeroPad(n, s) {
n = n.toString();
while (n.length < s) n = '0' + n;
return n;
}