#target photoshop
(function () {
if (app.documents.length === 0) {
try {
var folder1 = Folder.selectDialog("Select the JPEG folder:");
if (folder1 === null) {
alert('Script cancelled!');
return;
}
var folder2 = Folder.selectDialog("Select the PSD folder:");
if (folder2 === null) {
alert('Script cancelled!');
return;
}
var validateInputDir = (folder1.fsName === folder2.fsName);
if (validateInputDir === true) {
alert("Script cancelled as both the input folders are the same!");
return;
}
var list1 = folder1.getFiles(/\.(jpg|jpeg)$/i);
var list2 = folder2.getFiles(/\.(psd)$/i);
list1.sort();
list2.sort();
var validateEmptyList = (list1.length > 0 && list2.length > 0);
if (validateEmptyList === false) {
alert("Script cancelled as one of the input folders is empty!");
return;
}
var validateListLength = (list1.length === list2.length);
if (validateListLength === false) {
alert("Script cancelled as the input folders don't have equal quantities of images!");
return;
}
var outputFolder = Folder.selectDialog("Please select the output folder...");
if (outputFolder === null) {
return;
}
var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var counter = 0;
for (var i = 0; i < list1.length; i++) {
open(list1[i]);
open(list2[i]);
var docName = app.documents[1].name.replace(/\.[^\.]+$/, '');
var sourceDoc = app.documents[0];
var destinationDoc = app.documents[1];
app.activeDocument = sourceDoc;
sourceDoc.pathItems[0].select();
var copyPathItem = charIDToTypeID("copy");
executeAction(copyPathItem, undefined, DialogModes.NO);
app.activeDocument = destinationDoc;
var pastePathItem = charIDToTypeID("past");
executeAction(pastePathItem, undefined, DialogModes.NO);
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
var saveFile = File(outputFolder + "/" + docName + ".psd");
savePSD(saveFile);
while (app.documents.length) {
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
counter++;
function savePSD(saveFile) {
psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.embedColorProfile = true;
psdSaveOptions.alphaChannels = true;
psdSaveOptions.layers = true;
app.activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
}
}
app.displayDialogs = savedDisplayDialogs;
app.beep();
alert('Script completed!' + '\r' + counter + ' files saved to:' + '\r' + outputFolder.fsName);
} catch (error) {
while (app.documents.length > 0) {
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
alert("An unexpected error has occurred!");
}
} else {
alert('Please close all open documents before running this script!');
}
}());