ellockie
ellockie

Reputation: 4268

Illustrator script to recolor text

I'm trying to recolor text using Illustrator JavaScript. I'm trying to modify the script from the Select all objects with font-size between two sizes in illustrator? question:

doc = app.activeDocument;
tfs = doc.textFrames;
n = tfs.length; 

for ( i = 0 ; i < n ; i++ ) {
    alert(tfs[i].textRange.size); 
    // prints: TextType.POINTTEXT
    alert(tfs[i].textRange.fillcolor);
    // prints: undefined
}

I cannot get hold on the text color property. textRange object doesn't have such. I tried tfs[i].textRange.characters.fillcolor with the same result. How to get (and change) the text color?

Upvotes: 3

Views: 3821

Answers (1)

ellockie
ellockie

Reputation: 4268

Not intuitive, but it seems you cannot apply color to the whole text frame - you need to break it down into paragraphs and then apply .fillColor to each of them.

Below is my final script (without some extra stuff) that first checks if a given paragraph is of specific color (my marker), then it applies another one to it.

// define the desired color mode and value
var textColor_gray = new GrayColor();
textColor_gray.gray = 70;

var doc, tfs, n = 0, selectionArray = [], converted_counter = 0;

doc = app.activeDocument;
tfs = doc.textFrames;

// number of text frames
n = tfs.length;


// loop over text frames
for ( i = 0 ; i < n ; i++ )
{

    // To prevent errors with tfs[i].textRange.size when == 0
    if(tfs[i].textRange.length > 0)
    {
        // text frames counting
        selectionArray [ selectionArray.length ] = tfs[i];

        // get all the paragraphs from the current text frame
        var current_paragraphs = tfs[i].textRange.paragraphs;

        // loop over paragraphs
        for (j = 0; j < current_paragraphs.length; j++)
        {
            // proceed only with non-zero-length paragraphs
            if(current_paragraphs[j].characters.length > 0)
            {
                // check if the paragraph's color is of CMYK type
                if(current_paragraphs[j].fillColor instanceof CMYKColor)
                {
                    // if so then check if fill color's magenta component is > 99
                    if(current_paragraphs[j].fillColor.magenta > 99)
                    {
                        // convert it to gray
                        converted_counter++;
                        current_paragraphs[j].fillColor = textColor_gray;
                    }
                }
            }
        }
    }
}


// final messages
if ( selectionArray.length )
{
    app.selection = selectionArray;
    alert(selectionArray.length + "  text frames found (total: " + n + ").\nConverted " + converted_counter + " paragraphs.");
}
else
    alert("Nothing found in this range. Total: " + n);

Upvotes: 5

Related Questions