0

Inside indesign javascript scripting: how it is possible to change the color of a block of text instead the color of the bounding box of the block itself?

Because something like:

textFrame.fillColor = color;

would just change the color of the bounding box, not the text itself.

1 Answer 1

1

Because I raged so hard and the documentation is so poor and maybe even I myself will need it in future, I've decided to share knowledge by putting on a simple function to do it so:

My slower version:

function changeTextColor(textFrame, color) {

    var i, j;

    for (i = 0; i < textFrame.paragraphs.length; i++) { 

        p = textFrame.paragraphs[i]; 

        for(j = 0; j < p.lines.length; j++) {

            l = p.lines[j];

            l.fillColor = color;

        }

    }

}

Suggested improved versions by @mrdomino:

textFrame.textColumns.everyItem().fillColor = color
5
  • That seems needlessly loopy and probably much slower than it needs to be. A quick glance at the documentation (if you’re not, use the version on indesignjs.de, it’s by far the easiest to work out) leads me to believe that you should be able to just do textFrame.paragraphs.everyItem().fillColor = color without looping at all. Obviously everyItem() entails something looping somewhere, but it’s much more optimised and much faster than most loops you can make yourself. Jan 18, 2020 at 19:47
  • 1
    @JanusBahsJacquet, while this is much faster indeed this could result in some coloring of text outside the target text frame, namely if the last paragraph continues into a linked text frame. To avoid this, generally the fastest method that stays within the text frame only would be to use textColumns instead of paragraphs. So you could just do textFrame.textColumns.everyItem().fillColor = color. If you know you want to color the entire text, including linked frames, then the fastest method would be textFrame.parentStory.fillColor() = color.
    – mdomino
    Jan 18, 2020 at 20:48
  • 1
    @mdomino That is true about linked text, but that would also be the case with the loop solution given in the answer. Didn’t think to check whether parentStory had a fillColor property (note: property, not method, so no brackets), though, that’s even easier. Jan 18, 2020 at 20:52
  • True, just pointing out that this might not work in every case, depending on what the user wants. Good catch with the method, unfortunately I can't edit my comment anymore, so here's the correct version for easier copying: textFrame.parentStory.fillColor = color
    – mdomino
    Jan 18, 2020 at 20:55
  • Thanks both for the suggestions :) i will edit it Jan 18, 2020 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.