@AntonioPacheco – OK, I think that you need to change this line:
for (var m = theChannels.length-1; m >= 0; m--) {
to this:
for (var m = 0; m < theChannels.length; m++) {
Full code with very limited testing:
//code by c.pfaffenbichler
if (app.documents.length > 0) {
if (activeDocument.mode == DocumentMode.RGB || activeDocument.mode == DocumentMode.CMYK) {
var theChannels = collectChannels();
activeDocument.selection.deselect();
// create layers;
//for (var m = theChannels.length-1; m >= 0; m--) {
for (var m = 0; m < theChannels.length; m++) {
loadAndLayer(theChannels[m][1], theChannels[m][0], [0, 0, 0]);
app.doAction("action1","actionset")
}
app.doAction("action2","actionset")
}
}
////// collect layers //////
function collectChannels() {
// get number of layers;
var ref = new ActionReference();
//ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfChannels'));
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfChannels"));
// pixel dimensions;
var theWidth = applicationDesc.getUnitDoubleValue(stringIDToTypeID("width"));
var theHeight = applicationDesc.getUnitDoubleValue(stringIDToTypeID("height"));
var theRes = applicationDesc.getUnitDoubleValue(stringIDToTypeID("resolution"));
var thePixels = (theWidth * theRes / 72) * (theHeight * theRes / 72);
// process the channels;
var theChannels = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex(stringIDToTypeID("channel"), m);
var channelDesc = executeActionGet(ref);
// collect values;
var theName = channelDesc.getString(stringIDToTypeID('channelName'));
var theIndex = channelDesc.getInteger(stringIDToTypeID('itemIndex'));
var theID = channelDesc.getInteger(stringIDToTypeID('ID'));
if (channelDesc.hasKey(stringIDToTypeID('alphaChannelOptions')) === true) {
var theHistogram = channelDesc.getList(stringIDToTypeID('histogram'));
var value0 = theHistogram.getInteger(0);
// if not completely black;
if (value0 != thePixels) {
theChannels.push([theName, theIndex, theID]);
} else {}
}
} catch (e) {}
}
return theChannels;
}
//////
function loadAndLayer(theIndex, theName, theColor) {
var idnull = stringIDToTypeID("null");
var idcontentLayer = stringIDToTypeID("contentLayer");
var idchannel = stringIDToTypeID("channel");
var idmake = stringIDToTypeID("make");
// =======================================================
var desc11 = new ActionDescriptor();
var ref4 = new ActionReference();
ref4.putClass(idcontentLayer);
desc11.putReference(idnull, ref4);
var desc12 = new ActionDescriptor();
var desc13 = new ActionDescriptor();
var desc14 = new ActionDescriptor();
desc14.putDouble(stringIDToTypeID("red"), theColor[0]);
desc14.putDouble(stringIDToTypeID("grain"), theColor[1]);
desc14.putDouble(stringIDToTypeID("blue"), theColor[2]);
var idRGBColor = stringIDToTypeID("RGBColor");
desc13.putObject(stringIDToTypeID("color"), idRGBColor, desc14);
desc12.putString(stringIDToTypeID("name"), theName);
desc12.putObject(stringIDToTypeID("type"), stringIDToTypeID("solidColorLayer"), desc13);
desc11.putObject(stringIDToTypeID("using"), idcontentLayer, desc12);
executeAction(idmake, desc11, DialogModes.NO);
// remove layer mask;
try {
// =======================================================
var desc10 = new ActionDescriptor();
var ref2 = new ActionReference();
var idchannel = stringIDToTypeID("channel");
ref2.putEnumerated(idchannel, idchannel, stringIDToTypeID("mask"));
desc10.putReference(stringIDToTypeID("null"), ref2);
executeAction(stringIDToTypeID("delete"), desc10, DialogModes.NO);
} catch (e) {}
// =======================================================
var desc2 = new ActionDescriptor();
desc2.putClass(stringIDToTypeID("new"), idchannel);
var ref1 = new ActionReference();
ref1.putEnumerated(idchannel, idchannel, idmask = stringIDToTypeID("mask"));
desc2.putReference(stringIDToTypeID("at"), ref1);
desc2.putEnumerated(stringIDToTypeID("using"), stringIDToTypeID("userMaskEnabled"), stringIDToTypeID("hideAll"));
executeAction(idmake, desc2, DialogModes.NO);
// =======================================================
var desc3 = new ActionDescriptor();
var desc4 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putIndex(idchannel, theIndex);
desc4.putReference(stringIDToTypeID("to"), ref2);
var idpreserveTransparency = stringIDToTypeID("preserveTransparency");
desc4.putBoolean(stringIDToTypeID("invert"), true);
desc4.putBoolean(idpreserveTransparency, true);
desc3.putObject(stringIDToTypeID("with"), stringIDToTypeID("calculation"), desc4);
executeAction(idapplyImageEvent = stringIDToTypeID("applyImageEvent"), desc3, DialogModes.NO);
}