Skip to main content
Inspiring
April 11, 2018
Answered

Read and change specific words in a text layer

  • April 11, 2018
  • 1 reply
  • 3039 views

Hi

I have a text layer in Adobe Photoshop (it's a text-frame). Suppose I deffined it as a variable called "myText"... by using:

var myText = app.activeDocument.layers.getByName("dados");

In Adobe Illustrator, I can simply use the property: "words" in order to acess an array of all words in the text frame. I could declare anything like:

var countWords = myText.words;

And...after... I can acess the "countWords[1]", "countWords[2]".... to acess all individual words and personalize them.

Looking at Photoshop documentation I did not find similar property or object or instance... In "textItem" object there's no "paragraph" or "words" option that I can try.

Is there any way in Photoshop I can acess individual words in a text frame layer, so I could, for example, chance the color of this word...or increase the size of just this word?

Thank you very much for the help.

Gustavo.

This topic has been closed for replies.
Correct answer r-bin

You can change the text parameters for a specific

position and length using this function. The words and their positions in the text will have to be searched for.

var c1 = new SolidColor();

with (c1.rgb) { red   = 200; green = 100; blue = 150; }

var c2 = new SolidColor();

with (c2.rgb) { red   = 20; green = 100; blue = 200; }

// activeLayer in demo example must be textlayer

activeDocument.activeLayer.textItem.size = 12;

activeDocument.activeLayer.textItem.contents = "0123456789012345678901234567890";

// examples

set_text_style(0, 4, 50)

set_text_style(4, 4, 70, c1)

set_text_style(10, 5, 30, c2)

function set_text_style(from, len, size, color)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

        d.putReference(stringIDToTypeID("null"), r);

        var d1 = new ActionDescriptor();

        var list1 = new ActionList();

        var d2 = new ActionDescriptor();

        d2.putInteger(stringIDToTypeID("from"), from);

        d2.putInteger(stringIDToTypeID("to"), from+len);

        var d3 = new ActionDescriptor();

        d3.putUnitDouble(stringIDToTypeID("size"), stringIDToTypeID("pointsUnit"), size);

        if (color != undefined)

            {

            var d4 = new ActionDescriptor();

            d4.putDouble(stringIDToTypeID("red"),   color.rgb.red);

            d4.putDouble(stringIDToTypeID("green"), color.rgb.green);

            d4.putDouble(stringIDToTypeID("blue"),  color.rgb.blue);

            d3.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d4);

            }

        d2.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), d3);

        list1.putObject(stringIDToTypeID("textStyleRange"), d2);

        d1.putList(stringIDToTypeID("textStyleRange"), list1);

        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textLayer"), d1);

        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);

        }

    catch (e) { throw(e); }

    }

1 reply

pixxxelschubser
Community Expert
Community Expert
April 11, 2018

Hi Gustavo,

some snippets and or possible ways could be find here: Changing color of text found with regex

r-binCorrect answer
Legend
April 11, 2018

You can change the text parameters for a specific

position and length using this function. The words and their positions in the text will have to be searched for.

var c1 = new SolidColor();

with (c1.rgb) { red   = 200; green = 100; blue = 150; }

var c2 = new SolidColor();

with (c2.rgb) { red   = 20; green = 100; blue = 200; }

// activeLayer in demo example must be textlayer

activeDocument.activeLayer.textItem.size = 12;

activeDocument.activeLayer.textItem.contents = "0123456789012345678901234567890";

// examples

set_text_style(0, 4, 50)

set_text_style(4, 4, 70, c1)

set_text_style(10, 5, 30, c2)

function set_text_style(from, len, size, color)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

        d.putReference(stringIDToTypeID("null"), r);

        var d1 = new ActionDescriptor();

        var list1 = new ActionList();

        var d2 = new ActionDescriptor();

        d2.putInteger(stringIDToTypeID("from"), from);

        d2.putInteger(stringIDToTypeID("to"), from+len);

        var d3 = new ActionDescriptor();

        d3.putUnitDouble(stringIDToTypeID("size"), stringIDToTypeID("pointsUnit"), size);

        if (color != undefined)

            {

            var d4 = new ActionDescriptor();

            d4.putDouble(stringIDToTypeID("red"),   color.rgb.red);

            d4.putDouble(stringIDToTypeID("green"), color.rgb.green);

            d4.putDouble(stringIDToTypeID("blue"),  color.rgb.blue);

            d3.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d4);

            }

        d2.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), d3);

        list1.putObject(stringIDToTypeID("textStyleRange"), d2);

        d1.putList(stringIDToTypeID("textStyleRange"), list1);

        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textLayer"), d1);

        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);

        }

    catch (e) { throw(e); }

    }