Skip to main content
Inspiring
February 20, 2024
Answered

PS script for multiple actions for (files in) different folders automatic

  • February 20, 2024
  • 4 replies
  • 2486 views

Hi,

 

Can this be automated?

 

I am currently using 3 different PS recorded actions to adjust my photos with raw filter settings.

Each recorded action is for a specific type of folder (folder name known in advance). All the folders are always in a master folder (so together).

 

Now I just

choose script 1 and folder 1 and hit run

choose script 2 and folder 2 and hit run

choose script 3 and folder 3 and hit run

 

But is there a way to script this, so it runs automatically from script 1 to 3 (choosing each corresponding folder)?

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

@that35510420xudu 

 

You can try the following 1.2 version, remember to disable or remove the Save/Save As step from all 3 actions as this is now being performed by the script:

 

/*
Batch Play Actions from Conditional Folder Names v1-2.jsx
v1.0 - 21st February 2024, Stephen Marsh
v1.1 - 23rd February 2024, Replaced the original sub-folder/file processing code as it had unexpected errors in Windows (it worked on the Mac without issue)
v1.2 - 24th February 2024, Added a save to JPEG in a folder
https://community.adobe.com/t5/photoshop-ecosystem-discussions/ps-script-for-multiple-actions-for-files-in-different-folders-automatic/m-p/14435721
NOTE: 
* There must be 3 folders named: folder1, folder2, folder3
* The action set must be named: photo adjust
* There must be 3 actions named: action1, action2, action3
*/

#target photoshop

try {

    // Main script function
    (function () {

        // Save and disable dialogs
        var restoreDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.NO;

        // Recursive open files from 1st level sub-folders under the selected parent/root folder by c.pfaffenbichler (2022)
        var theFolder = Folder.selectDialog('Please select the input folder:');
        if (theFolder === null) return;
        var theFolders = theFolder.getFiles(isFolder);
        //theFolders.sort().reverse();
        theFolders.sort();

        for (var m = 0; m < theFolders.length; m++) {
            var inputFolder = theFolders[m];

            cTID = function (s) {
                return app.charIDToTypeID(s);
            };
            sTID = function (s) {
                return app.stringIDToTypeID(s);
            };

            var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif|webp)$/i);
            // inputFiles.sort().reverse();
            inputFiles.sort();

            // Validate that the file list is not empty
            var inputCount = inputFiles.length;
            var cancelScript = (inputCount === 0);
            if (cancelScript === true) {
                alert('Zero input files found, script cancelled!');
                return;
            }

            // Hide the Photoshop panels
            app.togglePalettes();

            for (var i = 0; i < inputFiles.length; i++) {

                app.open(File(inputFiles[i]));

                // Conditionally play the actions corresponding to the folder name
                try {
                    if (/folder1$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action1', 'photo adjust');
                        saveJPG();

                    } else if (/folder2$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action2', 'photo adjust');
                        saveJPG();

                    } else if (/folder3$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action3', 'photo adjust');
                        saveJPG();

                    } else {
                        alert('The idea is not to see this message...');
                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                    }
                } catch (e) {
                    alert('ERROR: Check that the action exists and is named correctly!');
                }
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            }
        }

        // Restore saved dialogs and panels
        app.displayDialogs = restoreDialogMode;
        app.togglePalettes();

        // End of script notification
        app.beep();
        alert('Script completed!');

        function isFolder(theFile) {
            if (theFile.constructor.name == "Folder") {
                return true;
            } else {
                return false;
            }
        }

        function saveJPG() {
            var docPath = app.activeDocument.path;
            var jpgFolder = new Folder(docPath + '/JPG/');
            if (!jpgFolder.exists) {
                jpgFolder.create();
            }
            var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
            var jpgSave = new File(jpgFolder + "/" + docName + ".jpg");
            var jpgOptns = new JPEGSaveOptions();
            jpgOptns.formatOptions = FormatOptions.STANDARDBASELINE;
            jpgOptns.embedColorProfile = true;
            jpgOptns.matte = MatteType.NONE;
            jpgOptns.quality = 12;
            app.activeDocument.saveAs(jpgSave, jpgOptns, true, Extension.LOWERCASE);
        }

    }());

} catch (e) {
    // Restore saved dialogs
    app.displayDialogs = restoreDialogMode;
    alert('Oops... Something went wrong!' + '\r' + e + ' ' + e.line);
}

4 replies

Stephen Marsh
Stephen MarshCorrect answer
Community Expert
February 24, 2024

@that35510420xudu 

 

You can try the following 1.2 version, remember to disable or remove the Save/Save As step from all 3 actions as this is now being performed by the script:

 

/*
Batch Play Actions from Conditional Folder Names v1-2.jsx
v1.0 - 21st February 2024, Stephen Marsh
v1.1 - 23rd February 2024, Replaced the original sub-folder/file processing code as it had unexpected errors in Windows (it worked on the Mac without issue)
v1.2 - 24th February 2024, Added a save to JPEG in a folder
https://community.adobe.com/t5/photoshop-ecosystem-discussions/ps-script-for-multiple-actions-for-files-in-different-folders-automatic/m-p/14435721
NOTE: 
* There must be 3 folders named: folder1, folder2, folder3
* The action set must be named: photo adjust
* There must be 3 actions named: action1, action2, action3
*/

#target photoshop

try {

    // Main script function
    (function () {

        // Save and disable dialogs
        var restoreDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.NO;

        // Recursive open files from 1st level sub-folders under the selected parent/root folder by c.pfaffenbichler (2022)
        var theFolder = Folder.selectDialog('Please select the input folder:');
        if (theFolder === null) return;
        var theFolders = theFolder.getFiles(isFolder);
        //theFolders.sort().reverse();
        theFolders.sort();

        for (var m = 0; m < theFolders.length; m++) {
            var inputFolder = theFolders[m];

            cTID = function (s) {
                return app.charIDToTypeID(s);
            };
            sTID = function (s) {
                return app.stringIDToTypeID(s);
            };

            var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif|webp)$/i);
            // inputFiles.sort().reverse();
            inputFiles.sort();

            // Validate that the file list is not empty
            var inputCount = inputFiles.length;
            var cancelScript = (inputCount === 0);
            if (cancelScript === true) {
                alert('Zero input files found, script cancelled!');
                return;
            }

            // Hide the Photoshop panels
            app.togglePalettes();

            for (var i = 0; i < inputFiles.length; i++) {

                app.open(File(inputFiles[i]));

                // Conditionally play the actions corresponding to the folder name
                try {
                    if (/folder1$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action1', 'photo adjust');
                        saveJPG();

                    } else if (/folder2$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action2', 'photo adjust');
                        saveJPG();

                    } else if (/folder3$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action3', 'photo adjust');
                        saveJPG();

                    } else {
                        alert('The idea is not to see this message...');
                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                    }
                } catch (e) {
                    alert('ERROR: Check that the action exists and is named correctly!');
                }
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            }
        }

        // Restore saved dialogs and panels
        app.displayDialogs = restoreDialogMode;
        app.togglePalettes();

        // End of script notification
        app.beep();
        alert('Script completed!');

        function isFolder(theFile) {
            if (theFile.constructor.name == "Folder") {
                return true;
            } else {
                return false;
            }
        }

        function saveJPG() {
            var docPath = app.activeDocument.path;
            var jpgFolder = new Folder(docPath + '/JPG/');
            if (!jpgFolder.exists) {
                jpgFolder.create();
            }
            var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
            var jpgSave = new File(jpgFolder + "/" + docName + ".jpg");
            var jpgOptns = new JPEGSaveOptions();
            jpgOptns.formatOptions = FormatOptions.STANDARDBASELINE;
            jpgOptns.embedColorProfile = true;
            jpgOptns.matte = MatteType.NONE;
            jpgOptns.quality = 12;
            app.activeDocument.saveAs(jpgSave, jpgOptns, true, Extension.LOWERCASE);
        }

    }());

} catch (e) {
    // Restore saved dialogs
    app.displayDialogs = restoreDialogMode;
    alert('Oops... Something went wrong!' + '\r' + e + ' ' + e.line);
}
Inspiring
February 24, 2024

thank you for the jpg folder adition, this is very helpfull. Thank you Stephen!

Stephen Marsh
Community Expert
February 23, 2024

@that35510420xudu 

 

Please try this updated script, now tested on Windows and Mac!

 

/*
Batch Play Actions from Conditional Folder Names v1-1.jsx
v1.0 - 21st February 2024, Stephen Marsh
v1.1 - 23rd February 2024, Replaced the original sub-folder/file processing code as it had unexpected errors in Windows (it worked on the Mac without issue)
https://community.adobe.com/t5/photoshop-ecosystem-discussions/ps-script-for-multiple-actions-for-files-in-different-folders-automatic/m-p/14435721
NOTE: 
* Each of the 3 actions must include a save command as the final step!
* There must be 3 folders named: folder1, folder2, folder3
* The action set must be named: photo adjust
* There must be 3 actions named: action1, action2, action3
*/

#target photoshop

try {

    // Main script function
    (function () {

        // Save and disable dialogs
        var restoreDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.NO;

        // Recursive open files from 1st level sub-folders under the selected parent/root folder by c.pfaffenbichler (2022)
        var theFolder = Folder.selectDialog('Please select the input folder:');
        if (theFolder === null) return;
        var theFolders = theFolder.getFiles(isFolder);
        //theFolders.sort().reverse();
        theFolders.sort();

        for (var m = 0; m < theFolders.length; m++) {
            var inputFolder = theFolders[m];

            cTID = function (s) {
                return app.charIDToTypeID(s);
            };
            sTID = function (s) {
                return app.stringIDToTypeID(s);
            };

            var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif|webp)$/i);
            // inputFiles.sort().reverse();
            inputFiles.sort();

            // Validate that the file list is not empty
            var inputCount = inputFiles.length;
            var cancelScript = (inputCount === 0);
            if (cancelScript === true) {
                alert('Zero input files found, script cancelled!');
                return;
            }

            // Hide the Photoshop panels
            app.togglePalettes();

            for (var i = 0; i < inputFiles.length; i++) {

                app.open(File(inputFiles[i]));

                // Conditionally play the actions corresponding to the folder name
                try {
                    if (/folder1$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action1', 'photo adjust');

                    } else if (/folder2$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action2', 'photo adjust');

                    } else if (/folder3$/i.test(app.activeDocument.path.fsName)) {
                        app.doAction('action3', 'photo adjust');

                    } else {
                        alert('The idea is not to see this message...');
                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                    }
                } catch (e) {
                    alert('ERROR: Check that the action exists and is named correctly!');
                }
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            }
        }

        // Restore saved dialogs and panels
        app.displayDialogs = restoreDialogMode;
        app.togglePalettes();

        // End of script notification
        app.beep();
        alert('Script completed!');

        function isFolder(theFile) {
            if (theFile.constructor.name == "Folder") {
                return true;
            } else {
                return false;
            }
        }

    }());

} catch (e) {
    // Restore saved dialogs
    app.displayDialogs = restoreDialogMode;
    alert('Oops... Something went wrong!' + '\r' + e + ' ' + e.line);
}

 

 

Inspiring
February 23, 2024

I have tested it, and as far as I can tell now it seems to work! I want to thank you for all your effort to make this script work. Really, thank you very much!

If I may add a small request. In the manual actions in PS, the adjusted files are added in a new folder named "JPG" in the folder of the files that are adjusted. Is there any change of adding that to this script?

Inspiring
February 23, 2024

so I mean: when the files is adjusted by action, save it as a copy in folder1/JPG. So a folder "JPG" inside folder1. And "JPG" inside folder2 for the folder2 adjusted files, etc.

Stephen Marsh
Community Expert
February 21, 2024

@that35510420xudu 

 

The following script requires that each of the 3 actions includes a save command as the final step!

 

/*
Batch Play Actions from Conditional Folder Names.jsx
v1.0 - 21st February 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/ps-script-for-multiple-actions-for-files-in-different-folders-automatic/m-p/14435721
NOTE: Each of the 3 actions must include a save command as the final step!
*/

#target photoshop

try {

    // Save and disable dialogs
    var restoreDialogMode = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Main script function
    (function () {

        // Select the input folder
        var inputFolder = Folder.selectDialog('Please select the top-level/root folder with sub-folders/files to process');
        if (inputFolder === null) return;

        // Limit the file format input, add or remove as required
        var filesAndFolders = scanSubFolders(inputFolder, /\.(png|jpg|jpeg|tif|tiff|tga|webp|psd|psb)$/i);
        // Set the recursive folder's files
        var fileList = filesAndFolders[0];
        // Recursive folder and file selection
        // Function parameters: folder object, RegExp or string
        function scanSubFolders(tFolder, mask) {
            /*
            Adapted from:
            https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-javascript-open-files-in-all-subfolders/m-p/5162230
            */
            var sFolders = [];
            var allFiles = [];
            sFolders[0] = tFolder;
            // Loop through folders
            for (var j = 0; j < sFolders.length; j++) {
                var procFiles = sFolders[j].getFiles();
                // Loop through this folder contents
                for (var i = 0; i < procFiles.length; i++) {
                    if (procFiles[i] instanceof File) {
                        if (mask == undefined) {
                            // If no search mask collect all files
                            allFiles.push(procFiles);
                        }
                        if (procFiles[i].fullName.search(mask) != -1) {
                            // Otherwise only those that match mask
                            allFiles.push(procFiles[i]);
                        }
                    } else if (procFiles[i] instanceof Folder) {
                        // Store the subfolder
                        sFolders.push(procFiles[i]);
                        // Search the subfolder
                        scanSubFolders(procFiles[i], mask);
                    }
                }
            }
            return [allFiles];
        }

        // Force alpha-numeric list sort
        fileList.sort().reverse();

        // Validate that the file list is not empty
        var inputCount = fileList.length;
        var cancelScript1 = (inputCount == 0);
        if (cancelScript1 === true) {
            alert('Zero input files found, script cancelled!');
            return;
        }

        // Set the file processing counter
        var fileCounter = 0;

        // Hide the Photoshop panels
        app.togglePalettes();

        // Process the files
        while (fileList.length) {

            for (var a = 0; a < 1; a++) {

                app.open(fileList.pop());

                // Conditionally play the actions corresponding to the folder name
                if (/\/folder1$/i.test(app.activeDocument.path.fsName)) {
                    app.doAction('action1', 'photo adjust');

                } else if (/\/folder2$/i.test(app.activeDocument.path.fsName)) {
                    app.doAction('action2', 'photo adjust');

                } else if (/\/folder3$/i.test(app.activeDocument.path.fsName)) {
                    app.doAction('action3', 'photo adjust');

                } else {
                    alert('The idea is not to see this message...')
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }

                // To avoid an infinite loop!
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                // Increment the file processing counter
                fileCounter++
            }
        }

        // Restore saved dialogs and panels
        app.displayDialogs = restoreDialogMode;
        app.togglePalettes();

        // End of script notification
        app.beep();
        alert('Script completed!' + '\n' + fileCounter + ' files processed!');

    }());

} catch (e) {
    // Restore saved dialogs
    app.displayDialogs = restoreDialogMode;
    alert('Oops... Something went wrong!' + '\r' + e + ' ' + e.line);
}

 

Enjoy!

 

Inspiring
February 21, 2024

I have now tested it and the script needs a little tinkering perhaps. When I run script and choose folder with subfolders, it shows message 

1. the idea is not to see this messsge..

2. Oops somthing went wrong, element does not excist 105.

 

Stephen Marsh
Community Expert
February 23, 2024

Ah, I see. Thank you very much for looking into it!


I believe that I have now found workable replacement code!

Stephen Marsh
Community Expert
February 20, 2024

A script can determine the name of the folder where the active image was opened.

 

Conditional logic can be used by a script to run an action based on the folder name.

 

The Script Events Manager can be used to automatically run a script when a file is opened.

 

I recall writing a similar script for someone on the forum, I'll see if I can dig it up. Edit:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-place-logos-in-different-phones-cases-in-photoshop/m-p/11098559

 

@that35510420xudu please upload a screenshot of the action set and action names. Also type or screenshot the 3 source folder names.

 

 

Inspiring
February 20, 2024

That would be great

 

folder1

folder2

folder3

Stephen Marsh
Community Expert
February 20, 2024

Do you want this to work in a batch? Or automatically if you randomly open a single file from the target folder?