Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


A script to run an action according to the file name

Community Beginner ,
Jul 26, 2017 Jul 26, 2017

Copy link to clipboard

Copied

Dear All,

I hope someone can help.

I am looking at the following task: I have several .jpg files in a folder. Their names are something like "ADE-4LP.jpg" or "DBA-3P.jpg" or "EFC-1P.jpg"

I would like to open these files in Photoshop and then run actions according to their file names. So for example, if the file name has the -4LP ending, the action named "Create4LP" is triggered. If the name has the -3P ending, the action "Create3P" is triggered and so on.

Can this be done with a script ?

Thanks muchly !!

Felix

TOPICS
Actions and scripting

Views

6.2K

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
community guidelines
Adobe Community Professional ,
Jul 26, 2017 Jul 26, 2017

Copy link to clipboard

Copied

Yes. A Script can process all open documents retrieve the documents names and compare the names. The scripts can play an Action by name if some compare result is positive. However the action must be able to work when there are other open documents and not mess with the other documents.  The actions need to be well behaved.

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
community guidelines
Community Beginner ,
Jul 26, 2017 Jul 26, 2017

Copy link to clipboard

Copied

Hi there, thanks for your reply to my thread. Who would write me such a script and how much would it cost ?

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
community guidelines
Adobe Community Professional ,
Jul 26, 2017 Jul 26, 2017

Copy link to clipboard

Copied

You want the script you should write it.

I have never hired anyone to write a script for me do not know what it would cost. What I need I hack myself. I fine script that do things close to what I want to do and modifty them or hack them myself.  I know Photoshop well but don't actually know javascript.  I look at others code and hammer at it.

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
community guidelines
Adobe Community Professional ,
Jul 26, 2017 Jul 26, 2017

Copy link to clipboard

Copied

You can try

Ps-Scripts

and see if someone can give you an estimate.

 

But as JJMack already mentioned you could also try to pick up the necessary JavaScript and try to combine functions from existing Scripts.

 

A thread on playing Actions:

Play Action - JS

One on having Photoshop edit Files in a Folder:

Opening files one by one

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
community guidelines
Adobe Community Professional ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

LATEST

I stumbled over this old post when looking for another, for what it is worth I decided to update the answer.

 

I modified the script that I created for another topic:

Script to place Logos in different phones cases in Photoshop

/*

https://community.adobe.com/t5/photoshop-ecosystem/a-script-to-run-an-action-according-to-the-file-name/m-p/9285996
A script to run an action according to the file name

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

ADE-4LP.jpg
DBA-3P.jpg
EFC-1P.jpg

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('-4LP') != -1) {

An alternative is to use a case insensitive regular expression based match:
    if (app.activeDocument.name.match(/-4LP/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('-4LP') != -1) {
                app.doAction('Create-4LP', 'Create Action Set'); // Change action & action set name
                /* CONDITION #1 */

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

                /* CONDITION #3 */
            } else if (app.activeDocument.name.indexOf('-1P') != -1) {
                app.doAction('Create-1P', 'Create 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
community guidelines
Community Beginner ,
Jul 26, 2017 Jul 26, 2017

Copy link to clipboard

Copied

yeah that helps tremendously

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
community guidelines
Adobe Community Professional ,
Jul 26, 2017 Jul 26, 2017

Copy link to clipboard

Copied

Actually the code presented in the last link should serve you pretty well …

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
community guidelines
Adobe Community Professional ,
Jul 26, 2017 Jul 26, 2017

Copy link to clipboard

Copied

I posted a script that processes open document  not too long ago I need an action to stack multiple images (opened in ps) into multiple layers If you do not want to open image files in a folder you could hack that script. Basic for loop forward or backwards

#target photoshop
app.bringToFront();

function main() {
	if (!documents.length) return;
	for(var i = 0; i < documents.length - 1; i++) { // for the open document
		app.activeDocument = documents[i]; // switch active document
		if (app.activeDocument.name.indexOf("-4LP.") != -1 ){doAction("Create4LP","actionsetName");}
		if (app.activeDocument.name.indexOf("-3P.") != -1 ){doAction("Create3P","actionsetName");}
	}
}
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
community guidelines
Adobe Community Professional ,
Jul 28, 2017 Jul 28, 2017

Copy link to clipboard

Copied

The following script will conditionally run an action, with filename being supported:

Siva's Photoshop Conditional Action: Siva's Photoshop Conditional Action

Download Siva's Photoshop Conditional Action

conditional.png

As there are only one set of Then/If conditions, you will only be able to process 1 filename per script run… However if you insert the script into an action, you can then have the Else condition fire up the script again to populate with the next filename etc to manually cycle through open filenames (untested, just an idea).

Enjoy!

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
community guidelines
Adobe Community Professional ,
Jul 28, 2017 Jul 28, 2017

Copy link to clipboard

Copied

It does not look like that script allows wild carding in file names which is what I think  the question is asking about.  It does seem to all contains but I do not know if the contains could be made the tail part of the name the is why I included the . as part of the search. in my script. Contains with extension and including a . may be as good as my attempt because I did not check to see if the dot found is the last dot  before the extension. However, the script only support one value not a list of values and a list of actions.

Capture.jpg

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
community guidelines
Adobe Community Professional ,
Jul 28, 2017 Jul 28, 2017

Copy link to clipboard

Copied

Hi JJMack, I guess these are valid concerns if you did not test the script against the sample filenames, however simply testing the OP’s filenames with the script appears to work with the criteria that the filename with/without extension contains the following key values in bold+red:

ADE-4LP.jpg | DBA-3P.jpg | EFC-1P.jpg

As I originally mentioned and as you say, it is going to be one condition at a time and not a list, so potentially many batches if there are many different codes in the file names. It is not a completely automated solution, however it is semi-automated and an “out of the box” solution that does not require any scripting knowledge by the end user. The project would be easily completed using this script long before the time that it took to learn scripting, find another free script to fully automate or wait on the good graces of another user to write a custom script.

P.S. As the notes at the original site suggest, a save step should also be included in the action being run.

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
community guidelines
Adobe Community Professional ,
Jul 29, 2017 Jul 29, 2017

Copy link to clipboard

Copied

Yes a simple action to batched. Step 1 would use a script to play the approbate action for the file opened step 2 save the modified documents. The Batch processor could also override the actions save as.... The script you posted does not seem to be a Plugin script it will alway be interactive.

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
community guidelines