我正在尝试使用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
同样的结果。如何获取(和更改)文本颜色?
答案 0 :(得分:2)
不直观,但似乎无法将颜色应用于整个文本框架 - 您需要将其分解为段落,然后将.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);