What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Reply to thread

Here is my take:


[CODE=javascript]/*

Batch Copy Paths from JPEG to PSD Files - 2 Input Folders.jsx

v1.0 - Stephen Marsh, 1st March 2023

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;

                // Get the first/top path from the JPEG file

                sourceDoc.pathItems[0].select();

                // Copy the selected path

                var copyPathItem = charIDToTypeID("copy");

                executeAction(copyPathItem, undefined, DialogModes.NO);

                // Set the active doc as the rendered Raw file

                app.activeDocument = destinationDoc;

                // Paste the copied path to destination file

                var pastePathItem = charIDToTypeID("past");

                executeAction(pastePathItem, undefined, DialogModes.NO);

                /*

                // Set the path to a clipping path

                activeDocument.pathItems[0].select();

                activeDocument.pathItems[0].makeClippingPath();

                */

                // 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 (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!');

    }

}());[/CODE]


What is our favorite program/app? (Hint - it begins and ends with the letter P)
Back
Top