Skip to main content
Inspiring
December 5, 2025
Question

Keyword replacement issue

  • December 5, 2025
  • 1 reply
  • 181 views

I want to replace 'Hello' with 'Good evening', but the color will be completely changed. How can I keep the original color?

01.jpg

var layer = app.activeDocument.activeLayer;
if (layer.kind == LayerKind.TEXT) {
    layer.textItem.contents = layer.textItem.contents.replace("Hello", "Good evening");
}

1 reply

Stephen Marsh
Community Expert
Community Expert
December 5, 2025

One option is to use AM code instead of DOM.

 

findReplaceText("Hello", "Good evening");

function findReplaceText(theFind, theReplace) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putProperty(s2t("property"), s2t("replace"));
    reference.putEnumerated(s2t("textLayer"), s2t("ordinal"), s2t("allEnum"));
    descriptor.putReference(s2t("null"), reference);
    descriptor2.putString(s2t("find"), theFind);
    descriptor2.putString(s2t("replace"), theReplace);
    descriptor2.putBoolean(s2t("checkAll"), true);
    descriptor2.putBoolean(s2t("forward"), true);
    descriptor2.putBoolean(s2t("caseSensitive"), false);
    descriptor2.putBoolean(s2t("wholeWord"), false);
    descriptor2.putBoolean(s2t("ignoreAccents"), true);
    descriptor.putObject(s2t("using"), s2t("findReplace"), descriptor2);
    executeAction(s2t("replace"), descriptor, DialogModes.NO);
}
Inspiring
December 5, 2025

Thank you for providing the code. I'm familiar with the built-in replacement feature in Photoshop. However, it has significant drawbacks—it's very slow and can cause errors if fonts are missing, leading to an abrupt stop. So, this is not what I'm looking for. I really appreciate your help.

c.pfaffenbichler
Community Expert
Community Expert
December 5, 2025

As @Stephen Marsh indicated editing a Type Layer with DOM code cannot preserve varied formating (including colors), so you can either use AM code or try UXP code.