• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Replace text in text layers

Explorer ,
Dec 02, 2017 Dec 02, 2017

Copy link to clipboard

Copied

I found a function from c.pfaffenbichler here on the forum, which will search/replace text. The script works great if you have a flat document, but as soon as you create a group the script won't work anymore.

Here's the function:

function replaceText (searchText, replaceWith)

{

    var actionDes1 = new ActionDescriptor();

    var actionRef = new ActionReference();

    actionRef.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID("replace") );

    actionRef.putEnumerated( charIDToTypeID( "TxLr" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Al  " ) );

    actionDes1.putReference( charIDToTypeID( "null" ), actionRef );

    var actionDes2 = new ActionDescriptor();

    actionDes2.putString( stringIDToTypeID( "find" ), searchText );

    actionDes2.putString( stringIDToTypeID("replace"), replaceWith );

    actionDes2.putBoolean( stringIDToTypeID( "checkAll" ), false );

    actionDes2.putBoolean( charIDToTypeID( "Fwd " ), true );

    actionDes2.putBoolean( stringIDToTypeID( "caseSensitive" ), false );

    actionDes2.putBoolean( stringIDToTypeID( "wholeWord" ), false );

    actionDes2.putBoolean( stringIDToTypeID( "ignoreAccents" ), true );

    actionDes1.putObject( charIDToTypeID( "Usng" ), stringIDToTypeID( "findReplace" ), actionDes2 );

    executeAction( stringIDToTypeID("replace"), actionDes1, DialogModes.NO );

};

replaceText ("test", "replaceWithStuff")

Here's the error I get:

---------------------------

Error

---------------------------

Error 8800: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

- The object "in contents of all text layer" is not currently available.

Line: 21

->      executeAction( stringIDToTypeID("replace"), actionDes1, DialogModes.NO );

---------------------------

OK  

---------------------------

Does anyone know how to work around this problem? I've tried a try/catch, the only thing that does is to suppress the error, but it won't replace the text 😕

TOPICS
Actions and scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Dec 02, 2017 Dec 02, 2017

I think we need adjust reference:

actionRef.putEnumerated( charIDToTypeID( "TxLr" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Al  " ) );

It should change all layers in document with layer kind "text"

So I would try this:

actionRef.putEnumerated( charIDToTypeID( "TxLr" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

This should change only all selected layers with kind "text". So it's up to you how you select only desired layers.

(not tested)

Votes

Translate

Translate
Adobe
Enthusiast ,
Dec 02, 2017 Dec 02, 2017

Copy link to clipboard

Copied

I think we need adjust reference:

actionRef.putEnumerated( charIDToTypeID( "TxLr" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Al  " ) );

It should change all layers in document with layer kind "text"

So I would try this:

actionRef.putEnumerated( charIDToTypeID( "TxLr" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

This should change only all selected layers with kind "text". So it's up to you how you select only desired layers.

(not tested)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 02, 2017 Dec 02, 2017

Copy link to clipboard

Copied

In order for your function to work you must stand on the text layer, regardless of whether the layer is in a group or not.

You can temporarily create a text layer,, call a function and then delete the temporary layer.

var layer0 = app.activeDocument.activeLayer;

var layer =  app.activeDocument.artLayers.add();

layer.kind = LayerKind.TEXT;

replaceText ("test", "replaceWithStuff") 

layer.remove();

app.activeDocument.activeLayer = layer0;

But in order for the replacement to be in all layers, you need to change the string in the function

actionDes2.putBoolean( stringIDToTypeID( "checkAll" ), false ); 

on this

actionDes2.putBoolean( stringIDToTypeID( "checkAll" ), true ); 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 03, 2017 Dec 03, 2017

Copy link to clipboard

Copied

LATEST

Thank you so much both of you, combining your input gives me exactly what I wanted

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines