为文本重新着色的 Illustrator 脚本

Illustrator script to recolor text

我正在尝试使用 Illustrator 为文本重新着色 JavaScript。我正在尝试修改 Select all objects with font-size between two sizes in illustrator? 问题中的脚本:

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
}

我无法控制文本颜色 属性。 textRange 对象没有这样的。我试过 tfs[i].textRange.characters.fillcolor 结果相同。如何获取(和更改)文本颜色?

不直观,但您似乎无法将颜色应用于整个文本框 - 您需要将其分解为段落,然后对每个段落应用 .fillColor

下面是我的最终脚本(没有一些额外的东西),它首先检查给定段落是否具有特定颜色(我的标记),然后应用另一个。

// 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);