Skip to main content
New Participant
January 10, 2020
Question

Javascript find and replace text in photoshop

  • January 10, 2020
  • 3 replies
  • 7542 views

Hi there,

 

I have been trying to write a script that finds and replaces certain text files in photoshop.

For example: There was a large man called (Name) with a big burly beard.

I have created an action using find and replace text to achieve this, but in the process of trying to automate this process by converting it to javascript using Xtools it no longer works.

Any help pointing me in the right direction would be super useful.

 

Cheers,

 

Marty

 

#target photoshop

//

// Action1.jsx

//

 

//

// Generated Fri Jan 10 2020 12:56:37 GMT+1100

//

 

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

 

//

//==================== Action 1 ==============

//

function Action1() {

  // Replace All

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Prpr'), sTID("replace"));

    ref1.putEnumerated(cTID('TxLr'), cTID('Ordn'), cTID('Al  '));

    desc1.putReference(cTID('null'), ref1);

    var desc2 = new ActionDescriptor();

    desc2.putString(cTID('find'), "(Name)");

    desc2.putString(sTID("replace"), "John");

    desc2.putBoolean(sTID("checkAll"), true);

    desc2.putBoolean(cTID('Fwd '), true);

    desc2.putBoolean(sTID("caseSensitive"), false);

    desc2.putBoolean(sTID("wholeWord"), false);

    desc2.putBoolean(sTID("ignoreAccents"), true);

    desc1.putObject(cTID('Usng'), sTID("findReplace"), desc2);

    executeAction(sTID('replace'), desc1, dialogMode);

  };

 

  step1();      // Replace All

};

 

 

 

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

//                    Action1.main

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

//

 

Action1.main = function () {

  Action1();

};

 

Action1.main();

 

// EOF

 

"Action1.jsx"

// EOF

 

This topic has been closed for replies.

3 replies

New Participant
February 18, 2021

Sorry I'm try  to use this script and this is the message: 

The command "Replace All" is not currently available

Any idea how  to  resolve that?? 😞

Brainiac
February 19, 2021

In order for this to work, any text layer must be active (selected). This is a long-standing bug for this command when used in Actions / Scripts.

Stephen Marsh
Adobe Expert
January 10, 2020

Here is the DOM code instead of AM code:

 

 

app.activeDocument.activeLayer.textItem.contents = app.activeDocument.activeLayer.textItem.contents.replace(/Dog/gi, 'Cat'); // g = global, i = case insensitve

 

 

While this version will change all text layers:

 

 

var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
   try {
      doc.activeLayer = doc.layers[i];
      // Find = Dog, Replace = Cat
      // g = global, i = case insensitve
      if (app.activeDocument.activeLayer.kind == LayerKind.TEXT) {
         doc.activeLayer.textItem.contents = doc.activeLayer.textItem.contents.replace(/\bDog\b/gi, 'Cat');
       }
   } catch (e) {}
}

 

 

EDIT: I forgot to add, if you wish to isolate the word Dog by itself as a "whole word" and not have it included within another word such as doggedly, then use the following modification, adding a \b either side of the target word:

 

 

 

 

.replace(/\bDog\b/gi, 'Cat');

 

 

 

 

 

AlexPrint
Inspiring
January 19, 2022

Hello!,

.replace(/\bDog\b/gi, 'Cat');

is an amazing code, thank you very much!, this is awesome and very usuable, I have one question,

is there any way it replace text after a word, I mean for example in series like:

Photo_01

Photo_02

Photo_BB

Image_03940

to remove the end in the 4 of them for example, I mean something like " _* "

I have an script to make a new text layer from the file name and I just want now to trim the text

after some character or series of words to keep only the important file as I must have names like

For sale _ Design 023.psd and I want the layers to be like "FOR SALE" only

I hope my question make sense, thank you guys for your help in advance, blessings.

Kukurykus
Brainiac
January 19, 2022

Is to much to ask for an example ?


I gave you one code, then second, to combine together with that first.

Isn't 'Image_Cat' result that you expected to have with that code?

JJMack
Adobe Expert
January 10, 2020

Yes the action conversion to Javascript by xtool does not work where the action does. 

The Scriptlistener Code works and the cleaned up code by Clean SL works.  Here is Dog to Cat

 

#target photoshop
//
// DoToCat.jsx
//
// =======================================================
// =======================================================
replace("Dog", "Cat", true, true, false, false, false);
function replace(find, replace, checkAll, forward, caseSensitive, wholeWord, ignoreAccents) {
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();

	reference.putProperty( stringIDToTypeID( "property" ), stringIDToTypeID( "replace" ));
	reference.putEnumerated( stringIDToTypeID( "textLayer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "allEnum" ));
	descriptor.putReference( charIDToTypeID( "null" ), reference );
	descriptor2.putString( stringIDToTypeID( "find" ), find );
	descriptor2.putString( stringIDToTypeID( "replace" ), replace );
	descriptor2.putBoolean( stringIDToTypeID( "checkAll" ), checkAll );
	descriptor2.putBoolean( stringIDToTypeID( "forward" ), forward );
	descriptor2.putBoolean( stringIDToTypeID( "caseSensitive" ), caseSensitive );
	descriptor2.putBoolean( stringIDToTypeID( "wholeWord" ), wholeWord );
	descriptor2.putBoolean( stringIDToTypeID( "ignoreAccents" ), ignoreAccents );
	descriptor.putObject( stringIDToTypeID( "using" ), stringIDToTypeID( "findReplace" ), descriptor2 );
	executeAction( stringIDToTypeID( "replace" ), descriptor, DialogModes.NO );
}

 

JJMack
Stephen Marsh
Adobe Expert
January 10, 2020

Thanks JJMack!

 

In case it was not obvious, the code in the previous post works on an active (text) layer.

 

Adding some extra code, all text layers that contain Dog can be replaced with Cat – this is just a quick hack, I have not used a conditional to only process text layers:

 

 

var doc = app.activeDocument;  
for(var i=0;i<doc.layers.length;i++){  
     try{  
          doc.activeLayer = doc.layers[i];
          // Find = Dog, Replace = Cat 
          replace("Dog", "Cat", true, true, false, false, false); // Change the 3rd boolean to true for case sensitive match  
     }  
     catch(e){}  
}  
  
function replace(find, replace, checkAll, forward, caseSensitive, wholeWord, ignoreAccents) {
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putProperty( stringIDToTypeID( "property" ), stringIDToTypeID( "replace" ));
	reference.putEnumerated( stringIDToTypeID( "textLayer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "allEnum" ));
	descriptor.putReference( charIDToTypeID( "null" ), reference );
	descriptor2.putString( stringIDToTypeID( "find" ), find );
	descriptor2.putString( stringIDToTypeID( "replace" ), replace );
	descriptor2.putBoolean( stringIDToTypeID( "checkAll" ), checkAll ); // Boolean #1
	descriptor2.putBoolean( stringIDToTypeID( "forward" ), forward ); // Boolean #2
	descriptor2.putBoolean( stringIDToTypeID( "caseSensitive" ), caseSensitive ); // Boolean #3
	descriptor2.putBoolean( stringIDToTypeID( "wholeWord" ), wholeWord ); // Boolean #4
	descriptor2.putBoolean( stringIDToTypeID( "ignoreAccents" ), ignoreAccents ); // Boolean #5
	descriptor.putObject( stringIDToTypeID( "using" ), stringIDToTypeID( "findReplace" ), descriptor2 );
	executeAction( stringIDToTypeID( "replace" ), descriptor, DialogModes.NO );
}