• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

If document name matches regex, play action

Explorer ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

I am frequently in a situation where, after finishing work on multiple open photoshop documents, I want to play a specific action on a few of them. The file name ALWAYS match a pattern that should be pretty easy to match with regex. Essentially:

If the document name is 5 or 6 digits + "F", play action A.

If the document name is 5 or 6 digits + "FX", play action B.

If the document name is 5 or 6 digits + "B", play action A.

If the document name is 5 or 6 digits + "BX", play action B.

I gather this requires a for loop to look through every open doc, and then a if docName matches regex, then play correct action.  But I am having trouble putting it all together and would greatly appreciate some assistance.

Thanks!

TOPICS
Actions and scripting

Views

563

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

correct answers 1 Correct answer

Community Expert , Jul 25, 2016 Jul 25, 2016

Hi squirpy​,

you can use a switch case or an if else clause. There is no difference.

// be sure that action set with name actionSet exists

// be sure that action1 in action set with name actionSet exists

for(i=0; i<app.documents.length; i++) {

    var aDoc = app.activeDocument = app.documents;

    if(aDoc.name.match(/^\d{5,6}(B|F)$/) != null) {

        // do action1

        app.doAction("action1", "actionSet");

    } else {

    if(aDoc.name.match(/^\d{5,6}((B|F)X)$/) != null) {

        // do action2

       

...

Votes

Translate

Translate
Adobe
Explorer ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

one more thing....the target documents are always unsaved so the regex DOES NOT need to contend with any extensions.

Votes

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
Guide ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

Make sure you amend the Action Name and ActionSetName!

#target photoshop;

//doAction("Action Name","ActionSetName");

//Change to suit!

for(d=0;d<app.documents.length;d++){

app.activeDocument = app.documents;

switch(app.activeDocument.name.match(/(^\d{5,6})(.*)/)[2].toLowerCase()){

case "f" : doAction("A","ActionSetName"); break;

case "fx" : doAction("B","ActionSetName"); break;

case "b" : doAction("A","ActionSetName");  break;

case "bx" : doAction("B","ActionSetName"); break;

default : break;

    }

};

Votes

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 Expert ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

Hi squirpy​,

you can use a switch case or an if else clause. There is no difference.

// be sure that action set with name actionSet exists

// be sure that action1 in action set with name actionSet exists

for(i=0; i<app.documents.length; i++) {

    var aDoc = app.activeDocument = app.documents;

    if(aDoc.name.match(/^\d{5,6}(B|F)$/) != null) {

        // do action1

        app.doAction("action1", "actionSet");

    } else {

    if(aDoc.name.match(/^\d{5,6}((B|F)X)$/) != null) {

        // do action2

        app.doAction("action2", "actionSet");

    } else {

        //alert("do nothing");

        }

    }

}

Have fun

Votes

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
Explorer ,
Jul 26, 2016 Jul 26, 2016

Copy link to clipboard

Copied

LATEST

Thanks to both of you for your assistance! It is VERY close to working perfectly. The problem is this....right now every time I run it, it misses some, but then if i run it again, it get's some more and so on.  So if I run it a few times it eventually gets them all, but not only this inefficient, needing to be run multiple times also will cripple my ability to add to the script and integrate it into my workflow.

The problems seems to be that my actions end by closing their target document. So documents.length is changing during the loop and throwing things off.  Is there a way to account for this by doing something with the iterator?

Here is my current code:

var actionfolder = 'regular';

var actionfolder2 = 'bottomFRAME';

var saveSquare = 'Save []';

var saveTemp = 'CATsend';

for(i=0; i<app.documents.length; i++) { 

    var aDoc = app.activeDocument = app.documents

    if(aDoc.name.match(/^\d{5,6}(B|F)$/) != null) { 

        app.doAction(saveTemp,actionfolder); 

    } else { 

    if(aDoc.name.match(/^\d{5,6}((B|F)X)$/) != null) {  

        app.doAction(saveSquare,actionfolder2); 

    } else { 

        alert("do nothing"); 

        } 

    } 

}

EDIT: The solution involves iterating backwards like so:

for (i = app.documents.length - 1; i >= 0; i--) { ... }

Now everything is working perfect....thanks again!

Votes

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