5 Replies Latest reply on Jul 24, 2017 1:53 AM by Stephen_A_Marsh

    Find and replace text in Photoshop files

    jitenderbhatia Level 1

      Hi

       

      I have 100 similar PSD files with same word written inside it. but name of the files is diffrent

       

      All 100 PSD files has one word written in it. for example. word is "TestWord"

       

      Name if the PSD files is like

       

      apple.psd

      banana.psd

      orange.psd

      peas.psd

      pears.psd

       

      now I want to replace "TestWord" with name of the files. like

       

      in apple.psd file, "TestWord" will be replaced with word "Apple"

      in banana.psd file, "TestWord" will be replaced with word "banana"

       

      please suggest some script to achieve this

       

      regards

        • 1. Re: Find and replace text in Photoshop files
          c.pfaffenbichler Level 9

          // 2017, use it at your own risk;

          #target photoshop

          if (app.documents.length > 0) {

          var myDocument = activeDocument;

          var theString = "TestWord";

          var theReplacementString = myDocument.name.match(/(.*)\.[^\.]+$/)[1];

          var textLayers = collectAndChangeTextLayers(app.activeDocument, [], theString, theReplacementString);

          };

           

           

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

          function collectAndChangeTextLayers (theParent, allLayers, theString, theReplacementString) {

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

            else {};

            var theNumber = theParent.layers.length - 1;

            for (var m = theNumber; m >= 0;m--) {

            var theLayer = theParent.layers[m];

          // apply the function to layersets;

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

          // edit typelayers;

            if (theLayer.kind == LayerKind.TEXT) {

            while (theLayer.textItem.contents.indexOf(theString) != -1) {theLayer.textItem.contents = theLayer.textItem.contents.replace(theString, theReplacementString)};

            allLayers.push(theLayer)

            }

            }

            else {

            allLayers = collectAndChangeTextLayers(theLayer, allLayers, theString, theReplacementString)

          // this line includes the layer groups;

          // allLayers.push(theLayer);

            }

            };

            return allLayers

            };

           

          (edited)

          1 person found this helpful
          • 2. Re: Find and replace text in Photoshop files
            jitenderbhatia Level 1

            hi c.pfaffenbichler

             

            one more question, assume, there is text file containing one word in each line . for example:

            apple

            banana

            orange

            pears

            peas

             

            I have a PSD file having multiple text layers, and only one word written in all layers, for example sample,

             

            so , looking for a script which take a word from txt file and replace this sample word and save the file

             

            I can create action for saving the file. but i need a script which can take word from the txt file one by one

             

            please help.

            • 3. Re: Find and replace text in Photoshop files
              jitenderbhatia Level 1

              hi

               

              can anyone help me with below query

               

              one more question, assume, there is text file containing one word in each line . for example:

              apple

              banana

              orange

              pears

              peas

               

              I have a PSD file having multiple text layers, and only one word written in all layers, for example sample,

               

              so , looking for a script which take a word from txt file and replace this sample word and save the file

               

              I can create action for saving the file. but i need a script which can take word from the txt file one by one

               

              please help.

              • 4. Re: Find and replace text in Photoshop files
                c.pfaffenbichler Level 9

                This is an example on how to use text-files via JavaScript.

                alert (readPref ("~/Desktop/aaa.txt"));

                ////// read prefs file //////

                function readPref (thePath) {

                  if (File(thePath).exists == true) {

                    var file = File(thePath);

                    file.open("r");

                    file.encoding= 'BINARY';

                    var theText = new String;

                    for (var m = 0; m < file.length; m ++) {

                      theText = theText.concat(file.readch());

                      };

                    file.close();

                    return String(theText)

                    }

                  };

                You could use the resulting Array in a for-clause.

                • 5. Re: Find and replace text in Photoshop files
                  Stephen_A_Marsh Adobe Community Professional

                  jitenderbhatia – If you can’t write the script yourself or rely on others generosity, then you may need to find an alternative method.

                   

                  One possibility is using Variables, otherwise known as “Data Driven Graphics”.

                   

                  There are many forum topics, webpages, videos etc out there:

                   

                  Creating data-driven graphics in Photoshop

                   

                  Good luck!

                  1 person found this helpful