Skip to main content
December 10, 2013
Question

Find and replace text in multiple Photoshop files?

  • December 10, 2013
  • 2 replies
  • 26821 views

Hi there,

Let us say I have six Photoshop files: 1.psd, 2.psd, ..., 6.psd. All of these files contain the word “LoremIpsum” in random text layers, within each document. Is there a way I can search for “LoremIpsum” in all documents and replace it with “Dolor Sit Amet”, all in one go? This is just an example, I need to replace various words, not just one.

I have tried "batch find and replace" software (including powerful tools like Power Grep) but they do not work with psd files… Is there a javascript of external plugin for this kind of task?

Thanks!

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
December 10, 2013

This should work on all open documents if the Type Layers’ content is uniform.

// replace text elements in type layers;

// 2013, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

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

                    app.activeDocument = app.documents;

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

                    }

          };

// the opertation;

function main () {

          var myDocument = app.activeDocument;

          var theLayers = collectTextLayers(myDocument, []);

          if (theLayers.length > 0) {

                    var theArray1 = ["LoremIpsum"];

                    var theArray2 = ["Dolor Sit Amet"];

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

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

                                        var theString = theLayers.textItem.contents;

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

                                                  theString = theString.replace(theArray1, theArray2)

                                                  };

                                        theLayers.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;

// 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

          };

Inspiring
September 13, 2017

I have 2 different words sets, this script is for single combination, can u plz modify... thanks in advance

c.pfaffenbichler
Community Expert
Community Expert
September 14, 2017

// replace text elements in type layers;

// 2017, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

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

                    app.activeDocument = app.documents;

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

                    }

          };

// the opertation;

function main () {

          var myDocument = app.activeDocument;

          var theArray = [["TEST", "123"], ["HAHAHA", "456"]];

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

                    replaceText (theArray[0], theArray[1])

                    }

          };

////// reoplace text //////

function replaceText (replaceThis, replaceWith) {

// =======================================================

var idreplace = stringIDToTypeID( "replace" );

    var desc22 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref3 = new ActionReference();

        var idPrpr = charIDToTypeID( "Prpr" );

        var idreplace = stringIDToTypeID( "replace" );

        ref3.putProperty( idPrpr, idreplace );

        var idTxLr = charIDToTypeID( "TxLr" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idAl = charIDToTypeID( "Al  " );

        ref3.putEnumerated( idTxLr, idOrdn, idAl );

    desc22.putReference( idnull, ref3 );

    var idUsng = charIDToTypeID( "Usng" );

        var desc23 = new ActionDescriptor();

        var idfind = stringIDToTypeID( "find" );

        desc23.putString( idfind, replaceThis );

        var idreplace = stringIDToTypeID( "replace" );

        desc23.putString( idreplace, replaceWith );

        var idcheckAll = stringIDToTypeID( "checkAll" );

        desc23.putBoolean( idcheckAll, true );

        var idFwd = charIDToTypeID( "Fwd " );

        desc23.putBoolean( idFwd, true );

        var idcaseSensitive = stringIDToTypeID( "caseSensitive" );

        desc23.putBoolean( idcaseSensitive, false );

        var idwholeWord = stringIDToTypeID( "wholeWord" );

        desc23.putBoolean( idwholeWord, false );

        var idignoreAccents = stringIDToTypeID( "ignoreAccents" );

        desc23.putBoolean( idignoreAccents, true );

    var idfindReplace = stringIDToTypeID( "findReplace" );

    desc22.putObject( idUsng, idfindReplace, desc23 );

executeAction( idreplace, desc22, DialogModes.NO );

};

c.pfaffenbichler
Community Expert
Community Expert
December 10, 2013

Are the individual Type Layers uniform or do they combine text elements of any variation (size, font, spacing, …)?

December 10, 2013

Thank you for the script! Will try to run it ASAP. The Type Layers are not uniform, font size, color, etc varies... Will the script work in this case?

c.pfaffenbichler
Community Expert
Community Expert
December 10, 2013

No, it won’t or rather is will destroy those variations.