6 Replies Latest reply on Aug 16, 2016 2:24 PM by pixxxel schubser

    conditional script by a specific part in the docname

    showshow Level 1

      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

        • 1. Re: conditional script by a specific part in the docname
          squirpy Level 1

          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[i];
              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--)
          
          • 2. Re: conditional script by a specific part in the docname
            pixxxel schubser MVP & Adobe Community Professional

            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.

            1 person found this helpful
            • 3. Re: conditional script by a specific part in the docname
              squirpy Level 1

              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.

              • 4. Re: conditional script by a specific part in the docname
                showshow Level 1

                So if i well understand it is not absoluty safe method ...?

                Thx any way

                • 5. Re: conditional script by a specific part in the docname
                  showshow Level 1

                  even if my structure is still like this

                  x=number

                  xxxxxxxxxxxxx_vx(1,2,3,4,5 max)_fgxxxxxxx_det.jpeg

                  ??

                  • 6. Re: conditional script by a specific part in the docname
                    pixxxel schubser MVP & Adobe Community Professional

                    A little bit more flexibility and secure:

                    // name_checkPartOfDocName.jsx
                    
                    // https://forums.adobe.com/thread/2194599
                    // conditional script by a specific part in the docname
                    // regards pixxxelschubser
                    
                    for (i = app.documents.length-1; i >= 0; i--) {
                    var aDoc = app.documents[i];
                    aDoc.name.match(/\d{13}_v([1-5])_/);
                    switch (RegExp.$1) {
                        case '1': v1 ();
                        break;
                        case '2': v2 ();
                        break;
                        case '3': v3 ();
                        break;
                        case '4': v4 ();
                        break;
                        case '5': v5 ();
                        break;
                        default: alert (txt = "Nothing found in: " + aDoc.name);
                        break;
                        }
                    }
                    
                    function v1 () {
                    alert ("v1 found in " + aDoc.name);
                    // do something
                    return;
                    }
                    function v2 () {
                    alert ("v2 found in " + aDoc.name);
                    // do something
                    return;
                    }
                    function v3 () {
                    alert ("v3 found in " + aDoc.name);
                    // do something
                    return;
                    }
                    function v4 () {
                    alert ("v4 found in " + aDoc.name);
                    // do something
                    return;
                    }
                    function v5 () {
                    alert ("v5 found in " + aDoc.name);
                    // do something
                    return;
                    }
                    

                     

                    Have fun