/*
Batch Copy All Paths from JPEG to PSD Files - 2 Input Folders.jsx
v1.0 - Stephen Marsh, 21st April 2024
Batch processing added to the following base script:
https://www.photoshopgurus.com/forum/threads/script-error-in-ps-2021.72619/
*/
#target photoshop
(function () {
if (app.documents.length === 0) {
try {
// JPG input folder
var folder1 = Folder.selectDialog("Select the JPEG folder:");
if (folder1 === null) {
alert('Script cancelled!');
return;
}
// PSD input folder
var folder2 = Folder.selectDialog("Select the PSD folder:");
if (folder2 === null) {
alert('Script cancelled!');
return;
}
// Validate input folder selection
var validateInputDir = (folder1.fsName === folder2.fsName);
if (validateInputDir === true) {
alert("Script cancelled as both the input folders are the same!");
return;
}
// Limit the file input to jpg/jpeg
var list1 = folder1.getFiles(/\.(jpg|jpeg)$/i);
var list2 = folder2.getFiles(/\.(psd)$/i);
// Alpha-numeric sort
list1.sort();
list2.sort();
// Validate that folder 1 & 2 lists are not empty
var validateEmptyList = (list1.length > 0 && list2.length > 0);
if (validateEmptyList === false) {
alert("Script cancelled as one of the input folders is empty!");
return;
}
// Validate that the item count in folder 1 & 2 matches
var validateListLength = (list1.length === list2.length);
if (validateListLength === false) {
alert("Script cancelled as the input folders don't have equal quantities of images!");
return;
}
// Select the output folder
var outputFolder = Folder.selectDialog("Please select the output folder...");
if (outputFolder === null) {
// alert('Script cancelled!');
return;
}
// or
/*
// Create the output sub-directory
var outputFolder = Folder(decodeURI(inputFolder + '/Paths Copied to PSD Files'));
if (!outputFolder.exists) outputFolder.create();
*/
// Save and set the dialog display settings
var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
// Start the file processing counter at zero
var counter = 0;
// Perform the processing
for (var i = 0; i < list1.length; i++) {
open(list1[i]);
open(list2[i]);
// Set the name of the save doc without extension
var docName = app.documents[1].name.replace(/\.[^\.]+$/, '');
// JPEG alphabetically sorts first [0]
var sourceDoc = app.documents[0];
// PSD alphabetically sorts second [1]
var destinationDoc = app.documents[1];
// Set the JPEG file as the active doc
app.activeDocument = sourceDoc;
// Forwards loop over the paths
for (var i = 0; i < sourceDoc.pathItems.length; i++) {
// Backwards loop over the paths
//for (i = sourceDoc.pathItems.length - 1; i > -1; i--) {
// Set the active doc as the JPEG file
app.activeDocument = sourceDoc;
// Select the path
thePath = sourceDoc.pathItems[i];
thePath.select();
// Copy the selected path
var copyPathItem = charIDToTypeID("copy");
executeAction(copyPathItem, undefined, DialogModes.NO);
// Set the active doc as the PSD file
app.activeDocument = destinationDoc;
// Paste the copied path to destination file
// NOTE: Clipping paths settings aren't retained!
var pastePathItem = charIDToTypeID("past");
executeAction(pastePathItem, undefined, DialogModes.NO);
// Deselect the paths
destinationDoc.pathItems[0].deselect();
/*
// Deselect the paths
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("path"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
executeAction(s2t("deselect"), descriptor, DialogModes.NO);
*/
}
// Close the JPEG file without saving any changes
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
// Set the save path options and save the rendered Raw file as PSD
var saveFile = File(outputFolder + "/" + docName + ".psd");
savePSD(saveFile);
// Close open files
while (app.documents.length) {
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
// Increment the file processing counter for each loop
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;
// End of script notification
app.beep();
alert('Script completed!' + '\r' + counter + ' files saved to:' + '\r' + outputFolder.fsName);
// outputFolder.execute();
} catch (e) {
while (app.documents.length > 0) {
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
//alert("An unexpected error has occurred!");
alert("Error!" + "\r" + e + ' ' + e.line);
}
} else {
alert('Please close all open documents before running this script!');
}
}());