I have some ideas and might be able to help, however, this may be a little advanced for my scripting level.
can you provide download links to 3 sets of images?
Here is my take, it could be optimized and have error checking added, however it works as expected in my tests using three random sets of image sequence numbers.
/* Combine Old - New - Mask Images to JPEG.jsx
//community.adobe.com/t5/photoshop/batch-process-groups-of-files-based-on-numbering/td-p/10809093
NOTE: There is no error checking, the 3 input folders must all contain the same quantity of images!
Input files are expected to have filenames such as:
OldImage0001.jpg, OldImage0002.jpg etc.
Mask0001.jpg, Mask0002.jpg etc.
NewImage0001.jpg, NewImage0002.jpg etc.
Output files will be modified from the input name:
FinishedImage0001.jpg, FinishedImage0002.jpg etc.
The mask data is based off the RGB composite channel as there was no other info to go by...
It is also assumed that the old/new/mask files all have the same width/height/resolution.
*/
// Prompt for input and output folders
var oldImages = Folder.selectDialog('Select the old images folder...', '~/desktop/'); // select the old images folder
var newImages = Folder.selectDialog('Select the new images folder...', '~/desktop/'); // select the new images folder
var maskImages = Folder.selectDialog('Select the mask images folder...', '~/desktop/'); // select the mask images folder
var outFolder = Folder.selectDialog('Select the save/output folder...', '~/desktop/'); // select the output images folder
// JPG Search Mask
var searchMask = '*.jpg';
var fileList1 = oldImages.getFiles(searchMask);
var fileList2 = newImages.getFiles(searchMask);
var fileList3 = maskImages.getFiles(searchMask);
// Force an alpha-numeric sort
fileList1.sort();
fileList2.sort();
fileList3.sort();
// JPEG Options
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 12; // Quality Level
jpegOptions.embedColorProfile = true; // or false
jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpegOptions.matte = MatteType.NONE;
// Input Loop
for (var i = 0; i < fileList1.length; i++) {
var doc1 = open(fileList1[i]);
var doc2 = open(fileList2[i]);
var doc3 = open(fileList3[i]);
// Start - Doing stuff to open files
var maskDoc = app.documents[2];
var newDoc = app.documents[1];
var oldDoc = app.documents[0];
app.activeDocument.selection.selectAll();
app.activeDocument.selection.copy(); // Copy the third image 'mask'
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = oldDoc; // Target first image 'old'
app.activeDocument.paste();
app.activeDocument = newDoc; // Target second image 'new'
app.activeDocument.selection.selectAll();
app.activeDocument.selection.copy(); // Copy the third image 'mask'
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = oldDoc; // Target first image 'old'
app.activeDocument.paste();
// Add layer mask (Clean SL)
makeLayermask();
function makeLayermask() {
var c2t = function (s) {
return app.charIDToTypeID(s);
};
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
descriptor.putClass(s2t("new"), s2t("channel"));
reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("mask"));
descriptor.putReference(s2t("at"), reference);
descriptor.putEnumerated(s2t("using"), c2t("UsrM"), s2t("revealAll"));
executeAction(s2t("make"), descriptor, DialogModes.NO);
}
// Apply image mask layer (Clean SL)
applyImage(true);
function applyImage(preserveTransparency) {
var c2t = function (s) {
return app.charIDToTypeID(s);
};
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("RGB"));
reference.putName(s2t("layer"), "Layer 1");
descriptor2.putReference(s2t("to"), reference);
descriptor2.putBoolean(s2t("preserveTransparency"), preserveTransparency);
descriptor.putObject(s2t("with"), c2t("Clcl"), descriptor2);
executeAction(s2t("applyImageEvent"), descriptor, DialogModes.NO);
}
app.activeDocument.activeLayer = app.activeDocument.layers.getByName("Layer 1");
app.activeDocument.activeLayer.remove();
// Finish - Doing stuff to open files
// Save JPEG
var docName = app.activeDocument.name.replace(/^Old/i, '');
app.activeDocument.flatten();
app.activeDocument.saveAs(new File(outFolder + '/' + 'Finished' + docName.split('.')[0] + '.jpg'), jpegOptions);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
alert('Script completed!');