Search code examples
javascriptphotoshopphotoshop-scriptdecrement

Photoshop Script Processing Images Out of Order


I am running javascript via Photoshop's scripts tool/option in order to batch process a folder of images. The script adds a second image layer from a single image in folder 02 to a number of images in folder 01. That works fine, however, I am trying to decrement the opacity of the layer that is added, so that the image that is added from folder 02 is increasingly more transparent for each subsequent image processed from folder 01. I'm basically creating a fade that transitions from the image in folder 02 to revealing the images processed from folder 01. The opacity change is happening as expected (decrementing opacity for each processed image); however, they seem to get processed out of order, so as I view the images in order, the opacity of that added layer jumps around. I can also see that as the newly processed images save to the processed image folder, they arrive out of order (something like 1, 3, 2, 5, 6, 4; though it might be different each time). I suspect I need to force my code to process the images one at a time in order, but I'm not sure how to go about that. Thanks in advance! I don't believe file naming is the issue since I've tried naming the images in folder 01 as both 1, 2, 3, (and so on) as well as 01, 02, 03. But that doesn't help.

Thanks in advance!

#target photoshop
// FOLDERS
var folder1 = new Folder('~/Desktop/1/');
var folder2 = new Folder('~/Desktop/2/');
var saveFolder = new Folder('~/Desktop/done/');
//
var searchMask = '*.???'; // get files named as this
var list1 = folder1.getFiles(searchMask);
var list2 = folder2.getFiles(searchMask);
var psdOptions = new PhotoshopSaveOptions();
psdOptions.layers = true;
opacityLevel = 100;


for (var i = 0; i < list1.length; i++) {
var doc = open(list1[i]);
var docName = doc.name;

placeFile('~/Desktop/2/1.jpg', 100); // for adding only one file each source file
doc.activeLayer.blendMode = BlendMode.LIGHTEN; // @@ change BLEND MODE

placeFile('~/Desktop/2/2.png', 100); // for adding only one file each source file
doc.activeLayer.blendMode = BlendMode.MULTIPLY; // @@ change BLEND MODE

doc.activeLayer.opacity = opacityLevel; // set layer opacity

// Decrement opacity level for each image
if (doc.activeLayer.opacity > 5) {  
    opacityLevel = Math.round(doc.activeLayer.opacity) - 5;
    } 

// SAVING
doc.saveAs(new File(saveFolder + '/' + docName.split('.')[0] + '.psd'), psdOptions);
doc.close(SaveOptions.DONOTSAVECHANGES);
};

function placeFile(file, scale) {
    try {
    var idPlc = charIDToTypeID("Plc ");
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc2.putPath(idnull, new File(file));
    var idFTcs = charIDToTypeID("FTcs");
    var idQCSt = charIDToTypeID("QCSt");
    var idQcsa = charIDToTypeID("Qcsa");
    desc2.putEnumerated(idFTcs, idQCSt, idQcsa);
    var idOfst = charIDToTypeID("Ofst");
    var desc3 = new ActionDescriptor();
    var idHrzn = charIDToTypeID("Hrzn");
    var idPxl = charIDToTypeID("#Pxl");
    desc3.putUnitDouble(idHrzn, idPxl, 0.000000);
    var idVrtc = charIDToTypeID("Vrtc");
    var idPxl = charIDToTypeID("#Pxl");
    desc3.putUnitDouble(idVrtc, idPxl, 0.000000);
    var idOfst = charIDToTypeID("Ofst");
    desc2.putObject(idOfst, idOfst, desc3);
    var idWdth = charIDToTypeID("Wdth");
    var idPrc = charIDToTypeID("#Prc");
    desc2.putUnitDouble(idWdth, idPrc, scale);
    var idHght = charIDToTypeID("Hght");
    var idPrc = charIDToTypeID("#Prc");
    desc2.putUnitDouble(idHght, idPrc, scale);
    var idAntA = charIDToTypeID("AntA");
    desc2.putBoolean(idAntA, true);
    executeAction(idPlc, desc2, DialogModes.NO);

    }
    catch (e) { }
}//end function placeFile  

Solution

  • Never count on file list functions to maintain a logical order...

    Try sorting the file lists after creating them.

    var list1 = folder1.getFiles(searchMask);
    var list2 = folder2.getFiles(searchMask);
    list1.sort();
    list2.sort();
    

    Make sure the filenames sort ascibetically. Zerofix if you use numbers.

    Alternatively, you can supply Array.sort() with a sorting function to allow for a custom sorting. Read more about how that works here.