Skip to main content
Participant
January 24, 2022
Answered

Batch Play Actions from Conditional Filename Parts + New Feature

  • January 24, 2022
  • 2 replies
  • 820 views

Hello there!

 

I'm hoping that someone can help me complete the script provided below. I found it in another discussion here and am having a bit of trouble trying to add a new feature to it.

 

I need it to duplicate any PSD file that ends with -3, retain its name, and be added to this action queue.

 

Explanation:

We have product photos that need to be ran through some actions in order to format them for our needs. We only take a photograph of 1 side of the product & manually go in to create a duplicate, rename it, and then run it through an action as well. This block of code seems to fit what we need ALMOST perfectly, but I'm not sure how to fit in the tedious bit where we duplicate the file that ends with "-3.psd" while allowing it to retain its name before the suffix & then be ran through its own droplet to flip the photo and change the suffix to "-2.psd". The possibility of this being done to the "-3.jpg" after the fact is fine too, I just need the final product to end with a new "-2.jpg" file that is a horizontally flipped version of the original "-3" view.

 

/*

Batch Play Actions from Conditional Filename Parts - Prompt for Suffix and JPEG Save Location.jsx

Changelog:
2nd May 2020 - Initial Release
3rd May 2020 - Added interactive JPEG save & suffix prompts

https://community.adobe.com/t5/photoshop/script-to-place-logos-in-diffrent-phones-cases-in-photshop/m-p/11098559
Script to place Logos in diffrent phones cases in Photshop

The following code example presumes that a folder of input files have a hyphen - delimiter with variable case-sensitive text, such as a colour name:

myfile-RED.psd
myfile-GREEN.psd
myfile-BLUE.psd

An action matching the specified matching portion of the filename would then be applied, overwriting the original files.

The indexOf method is case sensitive:
    if (app.activeDocument.name.indexOf('-RED') != -1) {

An alternative is to use a case insensitive regular expression based match:
    if (app.activeDocument.name.match(/-Red/gi) != null) {

*/

#target photoshop
app.bringToFront();

(function () {

    if (!documents.length) {

        var savedDisplayDialogs = app.displayDialogs;

        var inputFolder = Folder.selectDialog('Select the input folder:', ''); // Optionally add a default directory
        if (inputFolder == null) {
            return
        }; // Test if cancel returns null, then do nothing.

        var outputFolder = Folder.selectDialog('Select the JPEG output folder:', ''); // Optionally add a default directory
        if (outputFolder == null) {
            return
        }; // Test if cancel returns null, then do nothing.

        var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|psd|psb|eps|png|bmp|gif)$/i); // Add or remove as required

        app.displayDialogs = DialogModes.NO;

        for (var a = 0; a < inputFiles.length; a++) {
            try {
                var inDoc = open(inputFiles[a]);

                // START DOING STUFF

                    /* CONDITION #1 */
                if (app.activeDocument.name.indexOf('-0') != -1) {
                    app.doAction('Default View', 'View Set'); // Change action & action set name
                    /* CONDITION #1 */

                    /* CONDITION #2 */
                } else if (app.activeDocument.name.indexOf('-1') != -1) {
                    app.doAction('Default View', 'View Script'); // Change action & action set name
                    /* CONDITION #2 */

                    /* CONDITION #3 */
                } else if (app.activeDocument.name.indexOf('-3') != -1) {
                    app.doAction('Tilt View 3', 'View Script'); // Change action & action set name
                    /* CONDITION #3 */

                    /* CONDITION #4 */
                } else if (app.activeDocument.name.indexOf('-4') != -1) {
                    app.doAction('Default View', 'View Script'); // Change action & action set name
                    /* CONDITION #4 */

                } else {
                    /* DO SOMETHING ELSE */
                }

                var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                /* var saveFileJPEG = new File(outputFolder + '/' + docName + '_' + suffixCleansed + '.jpg');
                SaveForWeb(saveFileJPEG); */
                var doc = app.activeDocument;

                var jpgOptions = new JPEGSaveOptions();
                jpgOptions.quality = 12;
                jpgOptions.embedColorProfile = true;
                jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
                jpgOptions.scans = 5;
                jpgOptions.matte = MatteType.NONE;

                doc.saveAs (new File(outputFolder + '/' + fileName + '.jpg'), jpgOptions);

                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                // FINISH DOING STUFF

            } catch (e) {
                continue;
            }
        }

        app.displayDialogs = savedDisplayDialogs;
        alert('Script completed!' + '\n' + inputFiles.length + ' files saved to:' + '\n' + outputFolder.fsName);

    } else {
        alert('Please close all open files before running this script');
    }
})();

 

This topic has been closed for replies.
Correct answer Stephen Marsh

That script was mine. 

 

In condition 3, simply add an extra line of code within the else if block to duplicate the file with the same name, removing copy and the extension:

 

 

app.activeDocument.duplicate(app.activeDocument.name.replace(/\.[^\.]+$/, ''), false);

 

 

Note: This will make the new duped doc the active doc, with the original doc still open. If this does not suit your needs, then further code would need to be added to close the original doc:

 

 

var origDoc = app.activeDocument;
app.activeDocument.duplicate(app.activeDocument.name.replace(/\.[^\.]+$/, ''), false);
origDoc.close(SaveOptions.DONOTSAVECHANGES);

 

 

Example, I have placed it before the action:

 

 

                    /* CONDITION #3 */
                } else if (app.activeDocument.name.indexOf('-3') != -1) {
                    app.activeDocument.duplicate(app.activeDocument.name.replace(/\.[^\.]+$/, ''), false);
                    app.doAction('Tilt View 3', 'View Script'); // Change action & action set name
                    /* CONDITION #3 */

 

 

2 replies

Kukurykus
Legend
January 25, 2022

That must be something easy to do what you want, but let me understand better by posting some pictures...

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
January 25, 2022

That script was mine. 

 

In condition 3, simply add an extra line of code within the else if block to duplicate the file with the same name, removing copy and the extension:

 

 

app.activeDocument.duplicate(app.activeDocument.name.replace(/\.[^\.]+$/, ''), false);

 

 

Note: This will make the new duped doc the active doc, with the original doc still open. If this does not suit your needs, then further code would need to be added to close the original doc:

 

 

var origDoc = app.activeDocument;
app.activeDocument.duplicate(app.activeDocument.name.replace(/\.[^\.]+$/, ''), false);
origDoc.close(SaveOptions.DONOTSAVECHANGES);

 

 

Example, I have placed it before the action:

 

 

                    /* CONDITION #3 */
                } else if (app.activeDocument.name.indexOf('-3') != -1) {
                    app.activeDocument.duplicate(app.activeDocument.name.replace(/\.[^\.]+$/, ''), false);
                    app.doAction('Tilt View 3', 'View Script'); // Change action & action set name
                    /* CONDITION #3 */

 

 

t-eatonAuthor
Participant
January 26, 2022

Hey!! Your comments around this community have been super helpful as I've been digging around for my solution. Using the information you gave me, I was able to come to a solution; though I'm not sure it's 100% the best.

 

#target photoshop
app.bringToFront();

(function () {

    if (!documents.length) {

        var savedDisplayDialogs = app.displayDialogs;

        var inputFolder = Folder.selectDialog('Select the input folder:', ''); // Optionally add a default directory
        if (inputFolder == null) {
            return
        }; // Test if cancel returns null, then do nothing.

        var outputFolder = Folder.selectDialog('Select the JPEG output folder:', ''); // Optionally add a default directory
        if (outputFolder == null) {
            return
        }; // Test if cancel returns null, then do nothing.

        var inputFiles = inputFolder.getFiles(/\.(psd)$/i); // Add or remove as required

        app.displayDialogs = DialogModes.NO;

        /* JPG OPTIONS */
        var jpgOptions = new JPEGSaveOptions();
        jpgOptions.embedColorProfile = true;
        jpgOptions.formatOptions = FormatOptions.STANDARDBASELINE;
        jpgOptions.matte = MatteType.NONE;
        jpgOptions.quality = 12;
        jpgOptions.scans = 3;
        /* JPG OPTIONS */

        for (var a = 0; a < inputFiles.length; a++) {
            try {
                var inDoc = open(inputFiles[a]);

                // START DOING STUFF

                    /* CONDITION #1 */
                if (app.activeDocument.name.indexOf('-0') != -1) {
                    app.doAction('Default View', 'Photography Views'); // Change action & action set name
                    /* CONDITION #1 */

                    /* CONDITION #2 */
                } else if (app.activeDocument.name.indexOf('-1') != -1) {
                    app.doAction('Default View', 'Photography Views'); // Change action & action set name
                    /* CONDITION #2 */

                    /* CONDITION #3 */
                } else if (app.activeDocument.name.indexOf('-2') != -1) {
                    app.doAction('Tilt View 2', 'Photography Views'); // Change action & action set name
                    app.activeDocument.duplicate(app.activeDocument.name.replace('-2', '-3'), false);
                    app.doAction('Add View 3', 'Photography Views'); // Change action & action set name 
                    var newView = app.activeDocument.name.replace(/\.[^\.]+$/, ''); // ADD VARIABLE FOR NEW VIEW
                    app.activeDocument.saveAs(new File(outputFolder + '/' + newView + '.jpg'), jpgOptions); // SAVE NEW VIEW
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); // CLOSE NEW VIEW          
                    /* CONDITION #3 */

                    /* CONDITION #4 */
                } else if (app.activeDocument.name.indexOf('-4') != -1) {
                    app.doAction('Default View', 'Photography Views'); // Change action & action set name
                    /* CONDITION #4 */

                } else {
                    /* DO SOMETHING ELSE */
                    app.doAction('Default View', 'Photography Views'); // Change action & action set name
                }

                var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                // var saveFileJPEG = new File(outputFolder + '/' + docName + '_' + suffixCleansed + '.jpg');
                // SaveForWeb(saveFileJPEG); 


                app.activeDocument.saveAs(new File(outputFolder + '/' + docName + '.jpg'), jpgOptions); // SAVE JPG
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);  // CLOSE DOCUMENT


                // FINISH DOING STUFF

                /* JPEG S4W Options
                function SaveForWeb(saveFileJPEG, jpegQuality) {
                    // Convert to sRGB Step
                    if (app.activeDocument.colorProfileName.substring(0, 4) != "sRGB")
                    app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false); // BPC, Dither
                    var sfwOptions = new ExportOptionsSaveForWeb();
                    sfwOptions.format = SaveDocumentType.JPEG;
                    sfwOptions.includeProfile = true;
                    sfwOptions.optimized = true;
                    sfwOptions.quality = 70;
                    activeDocument.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
                } */

            } catch (e) {
                continue;
            }
        }

        app.displayDialogs = savedDisplayDialogs;
        alert('Script completed!' + '\n' + inputFiles.length + ' files saved to:' + '\n' + outputFolder.fsName);

    } else {
        alert('Please close all open files before running this script');
    }
})();

I put the duplicate & renaming of any file ending in a specific number within one of the 'conditions' & it all seems to work perfectly now. My only minor concerns are that the duplicated file isn't included in the script's ending alert, so the amount of files that it says it outputs doesn't match the true amount (which I suppose I could just reword) -- and I sadly couldn't figure out how to choose a default directory for the folder selection screens despite the fact that I feel like it's probably very simple & I'm failing at Google-ing. If you or anyone else have any comments on how to improve or implement these I'm all ears! This is my first time piecing together a script for Photoshop.

 

Thank you SO MUCH for your help & for being so active in this community.  I almost put "I need Philip J Fry's Help" in my original message since it was your script that I was reshaping for my needs.

Stephen Marsh
Community Expert
Community Expert
January 26, 2022

I'm glad that my comments helped!

 

On your two points:

 

* The end of script notification for file count is a bit of a hack, it assumes the input count number as the output count  number. This is a safe assumption as long as everything works correctly and or that you are not duplicating files. As you are duplicating files, then things get a little harder. The reason I did it this way was that I didn't know how to log processed files or if the chosen output folder was empty, or that if it did contain files, did some of them have the same filename extension. So if saving to an empty folder, one can easily  read in the count of files in that directory without having to filter the content (by filename pattern or extension etc). More info would be needed, can you elaborate?

 

* You are going to kick yourself, but the default directory is just a matter of filling in the missing paramater inbetween the second set of empty quotes.

 

I could have totally removed the empty quotes (as below)... however left them in as a "clue":

 

var outputFolder = Folder.selectDialog('Select the JPEG output folder:');

 

So change the original code from:

 

var outputFolder = Folder.selectDialog('Select the JPEG output folder:', ''); // Optionally add a default directory

 

To the relative path to the current user's desktop as an example:

 

var outputFolder = Folder.selectDialog('Select the JPEG output folder:', '~/Desktop');