Skip to main content
Participant
June 26, 2019
Answered

Set text layer color line by line

  • June 26, 2019
  • 1 reply
  • 675 views

Hi, New to photoshop scripting and trying to figure out if there is a way to iterate the contents of a text layer line by line, with the goal of setting each line to one of four colors. trying to run this on CS4 BTW.

It is easy enough to set the whole text layer color via code like this:

var textColour = new SolidColor();

textColour.rgb.hexValue="ff0000";

activeDocument.activeLayer.textItem.color = textColour;

But I can not seem to find an answer here for applying different colors within a single layer. I'm thinking (hoping) that you can iterate line by line using something like a .length property of the layer.

Any help would be greatly appreciated and apologies if this has been dealt with already.

This topic has been closed for replies.
Correct answer Chuck Uebele

You would have to use Action Manager Code to do this. You can record the code with scriptlistener, but will need to modify it to just get it down to line color. I'm not that good with modifying AM code. The other issue is that you will have to use carriage returns to define each line, so you can separate them. I don't think there is a way with word wrap.

1 reply

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
June 28, 2019

You would have to use Action Manager Code to do this. You can record the code with scriptlistener, but will need to modify it to just get it down to line color. I'm not that good with modifying AM code. The other issue is that you will have to use carriage returns to define each line, so you can separate them. I don't think there is a way with word wrap.

dune17856Author
Participant
June 28, 2019

Thank you for the quick reply Chuck. Late last night it was becoming clear that the functionality is just not there and AM would be painful. This morning I switched to a flow of scripting this functionality in InDesign (working), exporting to RTF, open RTF in Illustrator and Cut/Paste into photoshop which preserves the formatting. kludgey for sure but still beats manually alternating color of 150 lines of text, twice a week, for the customer in photoshop.

Indesign code (not perfect) if interested

var i, p, colorRED, colorBLU, colorGRN, colorBLK ;

colorRED = makeColor("cRED", ColorSpace.CMYK, ColorModel.process, [34, 98, 98, 63]);    //  102, 0, 0 Red  34, 98, 98, 63

colorBLU = makeColor("cBLU", ColorSpace.CMYK, ColorModel.process, [100, 98, 21, 31]);  // 0 0 102  Blue

colorGRN = makeColor("cGRN", ColorSpace.CMYK, ColorModel.process, [90, 34, 100, 27]);  // 0 102 51 Green

colorBLK = makeColor("cBLK", ColorSpace.CMYK, ColorModel.process, [75, 68, 67, 90]);    // 0 0 0    Black

for (i=0; i<app.selection[0].paragraphs.length; i++) {

    p = app.selection[0].lines;

    if (i%4 == 0){

        p.fillColor = colorRED;

    }

    if (i%4 == 1){

        p.fillColor = colorBLU;

    }

    if (i%4 == 2){

        p.fillColor = colorGRN;

    }

    if (i%4 == 3){

        p.fillColor = colorBLK;

    }

}

function makeColor(colorName, colorSpace, colorModel, colorValue) {

    var doc = app.activeDocument;

    var color = doc.colors.item(colorName);

    if (!color.isValid) {

        color = doc.colors.add({name: colorName, space: colorSpace, model: colorModel, colorValue: colorValue});

    }

    return color;

}


Unrelated side note. for some reason InDesign export to RTF get just one color wrong...