Skip to main content
showshow
Inspiring
August 12, 2016
Answered

conditional script by a specific part in the docname

  • August 12, 2016
  • 1 reply
  • 660 views

I've made something to have this  kind of stricture on picture i work :

0000000000001_v1_fg1208015_det.jpg

I would like to know if i can script anything from part of the name, for example a got a part named v1 is ( View 1),

but i got also, at the same place in the structure v2 or v3 etc...

My idea is to make a  script condition from this only part of the document name.

Thx for helping

This topic has been closed for replies.
Correct answer squirpy

If you want to do this one document at a time (or automate > batch it on a whole folder later) use this:

var workingName = app.activeDocument.name;

if(workingName.match(/(v1)/ig)){

    //do something

    } else if (workingName.match(/(v2)/ig)){

    //do something

    } else if (workingName.match(/(v3)/ig)){

    //do something

}

If you want to loop through all open documents use this:

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

    var curDoc = app.activeDocument = app.documents;

    var workingName = curDoc.name;

    if(workingName.match(/(v1)/ig)){

        //do something

        } else if (workingName.match(/(v2)/ig)){

        //do something

        } else if (workingName.match(/(v3)/ig)){

        //do something

        }

    }

NOTE: the 'i' toward the end of each regex pattern makes this case insensitive....if you want it to be case sensitive get rid of the 'i'. Also, in the second option if the "do something" part includes closing the document, this will throw off the loop. In that case change the iteration to count backwards like this:

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

1 reply

squirpyCorrect answer
Inspiring
August 12, 2016

If you want to do this one document at a time (or automate > batch it on a whole folder later) use this:

var workingName = app.activeDocument.name;

if(workingName.match(/(v1)/ig)){

    //do something

    } else if (workingName.match(/(v2)/ig)){

    //do something

    } else if (workingName.match(/(v3)/ig)){

    //do something

}

If you want to loop through all open documents use this:

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

    var curDoc = app.activeDocument = app.documents;

    var workingName = curDoc.name;

    if(workingName.match(/(v1)/ig)){

        //do something

        } else if (workingName.match(/(v2)/ig)){

        //do something

        } else if (workingName.match(/(v3)/ig)){

        //do something

        }

    }

NOTE: the 'i' toward the end of each regex pattern makes this case insensitive....if you want it to be case sensitive get rid of the 'i'. Also, in the second option if the "do something" part includes closing the document, this will throw off the loop. In that case change the iteration to count backwards like this:

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

pixxxelschubser
Community Expert
Community Expert
August 12, 2016

Hi squirpy​,

not bad. But you work with maximum risk.

Looking for (only) /(v1)/ can produce false positive hits.

e.g.

if the filename is not: 0000000000001_v1_fg1208015_det.jpg

rather is: 0000000000001_v1_fv1208015_det.jpg

A bit more safety give you the search with /_v1_/

(and much more safety with /\d{13}_v1_/ ) But nobody (except showshow) knows which possibilities in the kind of structure really exists.

Inspiring
August 12, 2016

Good call. As you mentioned, we don't know the specifics regarding anything that could produce false positives, but including the underscores is a no-brainer.