Illustrator Javascript:选择基于Swatch

时间:2012-06-28 20:00:07

标签: javascript adobe-illustrator

有没有制作一个Javascript Illustrator脚本来选择文档中具有特定颜色的所有内容?我在任何地方都找不到任何信息。我知道您可以使用“选择”下拉菜单执行我要求的操作,但我需要将其作为脚本的一部分。

我尝试过类似的事情:

myDoc.selection = Spot.name("CutContour");

myDoc.selection = myDoc.spots.item("CutContour");

但都不起作用。

2 个答案:

答案 0 :(得分:2)

这应该对你有所帮助。对象的fillColor属性不是对swatch对象的引用(因此我们可以检查其名称)。它描述了颜色本身(没有附加到样本面板的字符串)。说完之后,我们需要查找颜色类型名称及其值以匹配。但是,如果指定CutContour并且两种颜色具有此名称,则可能结果不是预期的结果。

function getObjectsByColor ( colorName )
{
var doc, items, i = 0, n = 0, item, color, selectionArray = [];

if ( app.documents.length == 0 ){ 
    alert("No documents open");
    return;
}

doc = app.activeDocument;
try
{
    color = doc.swatches.getByName ( colorName );
}
catch(e)
{
    alert( "No such color !");
    return;
}

color = color.color ;

items = doc.pageItems;
n = items.length;
if ( items.length == 0 )
{
    alert( "No items found");
    return;
}

for ( i = 0; i < n ; i++ )
{
    item = items[i];
    if ( item.fillColor.typename == color.typename
    && item.fillColor.cyan == color.cyan
    && item.fillColor.magenta == color.magenta
    && item.fillColor.yellow == color.yellow
    && item.fillColor.black == color.black )
    {
        selectionArray [ selectionArray.length ] = item;
    }
}

if ( selectionArray.length == 0 )
{
    alert( "Nothing found" );
    return;
}

app.selection = selectionArray;
}

getObjectsByColor ("CutContour");

卢瓦克

PS:您使用的是Caldera RIP吗?

答案 1 :(得分:0)

是的,有办法解决这个问题。此脚本与“选择相同填充颜色”相同的操作在Illustrator中执行。评论代码以及您的帮助。它在Illustrator CC 2017中完美运行

app.selection = null;
if (app.activeDocument.length > 0) {
    try {
        var swatch = app.activeDocument.swatches.getByName('CMYK Red');
        var temp = app.documents[0].pathItems.rectangle(10, 10, 150, 150);
        temp.fillColor = swatch.color;

        // To select object that have CMYK Red swatch applied to stroke and fill.
        app.executeMenuCommand('Find Fill & Stroke menu item');

        // To select object that have CMYK Red swatch applied to fill.
        app.executeMenuCommand('Find Fill Color menu item');

        // To select object that have CMYK Red swatch applied to stroke.
        app.executeMenuCommand('Find Stroke Color menu item');

        temp.remove();
    } catch (e) {
        alert('CMYK Red swatch does not exists!!')
    }
}