Highlighted

Conditional Batch Processing using Script

New Here ,
Nov 16, 2020

Copy link to clipboard

Copied

Hey, so was wondering if there is a way to batch process a folder structure BUT only open the a given file if a condition is met (in my case: I only want to open files with a certain keyword in the file name).

 

There is a option in Batch Processing to just open files when the Action says so, the problem is that I don't know how to access the current filename without opening it. "app.activeDocument.name" does not work at that point.

 

Someone an idea?

Adobe Community Professional
Correct answer by Stephen_A_Marsh | Adobe Community Professional

I wrote a script for this task for somebody in this topic thread:

 

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

 

Should be easy enough to modify...

 

Code repeated below, just in case the forum links get broken in the future:

 

/*

Batch Play Actions from Conditional Filename Parts.jsx

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();

if (!documents.length) {

    var savedDisplayDialogs = app.displayDialogs;

    var inputFolder = Folder.selectDialog('Select the input folder', '');
    var inputFiles = inputFolder.getFiles();

    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('-RED') != -1) {
                app.doAction('Red Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #1 */

                /* CONDITION #2 */
            } else if (app.activeDocument.name.indexOf('-GREEN') != -1) {
                app.doAction('Green Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #2 */

                /* CONDITION #3 */
            } else if (app.activeDocument.name.indexOf('-BLUE') != -1) {
                app.doAction('Blue Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #3 */

            } else {
                /* DO SOMETHING ELSE */
            }

            app.activeDocument.close(SaveOptions.SAVECHANGES);

            // Finish doing stuff

        } catch (e) {
            continue;
        }
    }

    app.displayDialogs = savedDisplayDialogs;
    alert('Script completed!' + '\n' + 'Files saved to:' + '\n' + inputFolder.fsName);

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

Views

51

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Conditional Batch Processing using Script

New Here ,
Nov 16, 2020

Copy link to clipboard

Copied

Hey, so was wondering if there is a way to batch process a folder structure BUT only open the a given file if a condition is met (in my case: I only want to open files with a certain keyword in the file name).

 

There is a option in Batch Processing to just open files when the Action says so, the problem is that I don't know how to access the current filename without opening it. "app.activeDocument.name" does not work at that point.

 

Someone an idea?

Adobe Community Professional
Correct answer by Stephen_A_Marsh | Adobe Community Professional

I wrote a script for this task for somebody in this topic thread:

 

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

 

Should be easy enough to modify...

 

Code repeated below, just in case the forum links get broken in the future:

 

/*

Batch Play Actions from Conditional Filename Parts.jsx

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();

if (!documents.length) {

    var savedDisplayDialogs = app.displayDialogs;

    var inputFolder = Folder.selectDialog('Select the input folder', '');
    var inputFiles = inputFolder.getFiles();

    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('-RED') != -1) {
                app.doAction('Red Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #1 */

                /* CONDITION #2 */
            } else if (app.activeDocument.name.indexOf('-GREEN') != -1) {
                app.doAction('Green Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #2 */

                /* CONDITION #3 */
            } else if (app.activeDocument.name.indexOf('-BLUE') != -1) {
                app.doAction('Blue Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #3 */

            } else {
                /* DO SOMETHING ELSE */
            }

            app.activeDocument.close(SaveOptions.SAVECHANGES);

            // Finish doing stuff

        } catch (e) {
            continue;
        }
    }

    app.displayDialogs = savedDisplayDialogs;
    alert('Script completed!' + '\n' + 'Files saved to:' + '\n' + inputFolder.fsName);

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

Views

52

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 16, 2020 0
Most Valuable Participant ,
Nov 16, 2020

Copy link to clipboard

Copied

A script can get a list of file in a file system tree and process that list  to see if the file you want to use in you process exists. What  I do not understand is "Just open files when the Action says so"  I do not think Actions can speak..

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 16, 2020 0
Adobe Community Professional ,
Nov 16, 2020

Copy link to clipboard

Copied

I wrote a script for this task for somebody in this topic thread:

 

https://community.adobe.com/t5/photoshop/script-to-place-logos-in-different-phones-cases-in-photosho...

 

Should be easy enough to modify...

 

Code repeated below, just in case the forum links get broken in the future:

 

/*

Batch Play Actions from Conditional Filename Parts.jsx

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();

if (!documents.length) {

    var savedDisplayDialogs = app.displayDialogs;

    var inputFolder = Folder.selectDialog('Select the input folder', '');
    var inputFiles = inputFolder.getFiles();

    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('-RED') != -1) {
                app.doAction('Red Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #1 */

                /* CONDITION #2 */
            } else if (app.activeDocument.name.indexOf('-GREEN') != -1) {
                app.doAction('Green Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #2 */

                /* CONDITION #3 */
            } else if (app.activeDocument.name.indexOf('-BLUE') != -1) {
                app.doAction('Blue Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #3 */

            } else {
                /* DO SOMETHING ELSE */
            }

            app.activeDocument.close(SaveOptions.SAVECHANGES);

            // Finish doing stuff

        } catch (e) {
            continue;
        }
    }

    app.displayDialogs = savedDisplayDialogs;
    alert('Script completed!' + '\n' + 'Files saved to:' + '\n' + inputFolder.fsName);

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 16, 2020 1
New Here ,
Nov 17, 2020

Copy link to clipboard

Copied

Perfect, that was missing. 

Here's what I made out of it:

/*
batch process files in a folder structure if condision is met.
In this case all files containing the fileNameSearchTerm are beeing opened and saved elsewhere.
https://community.adobe.com/t5/photoshop/conditional-batch-processing-using-script/m-p/11599420

partially Based on:
https://community.adobe.com/t5/photoshop/script-to-place-logos-in-diffrent-phones-cases-in-photshop/m-p/11098559
*/

#target photoshop
	
	app.bringToFront();

	var doc = app.activeDocument;

	var fileNameSearchSwitch = true;
	var fileNameSearchTerm = "_fileToProcess";

    var inputFolder = Folder.selectDialog('Select the input folder', '');
    //var outputFolder = Folder.selectDialog('Select the output folder', '');
	var outputFolder = "C:/Users/Workstation/Desktop/Test/";

    var inputFiles = findAllFiles(inputFolder);
	

	//GetAllFiles
	function findAllFiles(theFolder) {
		
		var fileArray = [];
		
		iterateFolder(theFolder);
		function iterateFolder(theFolder){
			
			var fileFolderArray = theFolder.getFiles();
			
			for ( var i = 0; i < fileFolderArray.length; i++ ) {
				
				var fileFoldObj = fileFolderArray;
				var currentFile = fileFolderArray[i];
				
				if (currentFile instanceof File ){
					if(fileNameSearchSwitch && currentFile.name.indexOf(fileNameSearchTerm) != -1 || !fileNameSearchSwitch){
						fileArray.push(currentFile);
						doStuff(currentFile);
					}
				}else{iterateFolder(currentFile);}
				
			}
		}
		
	}


	//Main Function
	function doStuff(currentFile){
		
		var doc = open(currentFile);
		safeTIFtoNewLocation(doc);
		
	}



	//Safe TIF example
	function safeTIFtoNewLocation(doc){
		
		//Get File Name without extension
		var fileName = doc.name.split(".")[0];
		
		//Get File Path without name
		var filePath = doc.fullName.toString().slice( 0, doc.name.length * -1); 

		//Make new filePath from Input and Output folders
		var filePath = filePath.split(inputFolder.name); 
		var filePath =  outputFolder + filePath[1];
		
		//Check if filePath it exist, if not create it.
		var folder1 = Folder(filePath);
		if(!folder1.exists) folder1.create();

		//Make full save Path
		var filePathAndName = filePath + fileName;

		//Save
		var saveFile = new File( filePathAndName );
		tiffSaveOptions = new TiffSaveOptions();   
		tiffSaveOptions.embedColorProfile = true;   
		tiffSaveOptions.alphaChannels = true;   
		tiffSaveOptions.layers = true;  
		tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
		activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);   
		
		// close without saving
		doc.close(SaveOptions.DONOTSAVECHANGES);

	}
	

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 17, 2020 0
Adobe Community Professional ,
Nov 16, 2020

Copy link to clipboard

Copied

Yet another option is to use Adobe Bridge to filter/limit the list of files based on the name or some other criteria, then process the selected sub-set of files in Photoshop.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 16, 2020 0