1 Reply Latest reply on Sep 13, 2017 8:34 AM by c.pfaffenbichler

    script to find and replace in all document

    abhijeett89122812 Level 1

      I have the below script which find and replace only only one sentence I want to 2 different sentence should be find and replaced, need modification, thanks in advance

       

      #target photoshop

      if (app.documents.length > 0) {

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

                          app.activeDocument = app.documents[n];

                          app.activeDocument.suspendHistory("replace text", "main()")

                          }

                };

      // the opertation;

      function main () {

                var myDocument = app.activeDocument;

                var theLayers = collectTextLayers(myDocument, []);

                if (theLayers.length > 0) {

                          var theArray1 = ["FIND OUT MORE"];

                          var theArray2 = ["ĎALŠIE INFORMÁCIE"];

                          for (var a = 0; a < theLayers.length; a++) {

                                    for (var b = 0; b < theArray1.length; b++) {

                                              var theString = theLayers[a].textItem.contents;

                                              while (theString.indexOf(theArray1[b]) != -1) {

                                                        theString = theString.replace(theArray1[b], theArray2[b])

                                                        };

                                              theLayers[a].textItem.contents = theString;

                                              }

                                    }

                          }

                };
         

      ////// function collect all layers //////

      function collectTextLayers (theParent, allLayers) {

                if (!allLayers) {var allLayers = new Array};

                else {};

                for (var m = theParent.layers.length - 1; m >= 0;m--) {

                          var theLayer = theParent.layers[m];

      // apply the function to layersets;

                          if (theLayer.typename == "ArtLayer") {

                                    if (theLayer.kind == LayerKind.TEXT) {allLayers.push(theLayer)};

                                    }

                          else {

                                    allLayers = (collectTextLayers(theLayer, allLayers))

      // this line includes the layer groups;

      //                              allLayers.push(theLayer);

                                    }

                          };

                return allLayers

                };