Illustrator在复合路径内分组对象

时间:2013-10-18 20:04:43

标签: javascript adobe-illustrator extendscript

我遇到了Illustrator CS6的脚本问题,这个问题一直困扰着我很长一段时间。复合路径内的组。

我已经设置了一个脚本来提取Illustrator文件中的所有使用的颜色,以及半色调信息,CMYK等。它基本上循环遍历文件中的每个路径并查看其填充颜色(如果它有任何),笔触颜色(如果有的话),渐变停止(如果有的话),等等。如果它正在查看的项目是一个组,它只是潜入该组并通过递归调用它来查看其所有组件功能相同。复合路径也是如此。它还设置为处理大多数光栅图像,报告其彩色位图或CMYK图像等。

然而,当脚本失败时,它会遇到包含组的复合路径。现在,通常,这在Illustrator中甚至是不可能的。如果您尝试创建一组两个路径,然后复合它们,Illustrator只会删除分组。但是,有些人使用的程序,当从它们导出到EPS文件时,某些路径最终成为复合路径内的组。除了训练我的所有人来寻找这些问题路径之外,我还能用脚本做些什么吗?这是我目前的脚本:

/**
 * The main part of the script that will run in Illustrator, getting the text of the object we're looking for.
 *
 * @param {File} theFile The file object that will be opened in Illustrator and checked.
 */
function findInfo(theFile)
{
    var document = app.open(theFile);
    var prodInfo = new Array;

    // This first section of the Illustrator script will just get the template name of the current product.
    var templateName = new String;

    var templateLayer = document.layers[2];
    $.writeln(templateLayer.name);
    for (var i = templateLayer.pageItems.length - 1; i >= 0; i--)
    {
        var pName = templateLayer.pageItems[i].name;
        if (pName != "")
        {
            templateName = templateLayer.pageItems[i].name;
        }
    }

    $.writeln("templateName (inside Illustrator Script) is " + templateName);

    // This second section of the Illustrator script will gather all of the used colors and store them in an array.
    var colorsArray = [];
    var bHalftones = false;
    var bFourCP = false;
    var bReg = false;
    colorsInUse(document.layers[0]);

    function colorsInUse(currPageItem) {
        for (var i = 0; i < currPageItem.pageItems.length; i++) {
            // Stepping through each item on the layer.
            var currentItem = currPageItem.pageItems[i];
            // $.writeln("current item is " + currentItem.typename);
            // $.writeln("Does it have a fill color? " + currentItem.fillColor);
            if (currentItem.typename === "GroupItem" && !currentItem.guides) {
                // If it's a group, dig into the group and start the function over.
                colorsInUse(currentItem);
            } else if (currentItem.typename == "TextFrame") {
                var charAttrib = currentItem.textRange.characterAttributes;
                getColors(charAttrib, colorsArray);
            } else if (currentItem.typename === "RasterItem") {
                if (currentItem.imageColorSpace === ImageColorSpace.CMYK) {
                    $.writeln("Four-color process image in artwork.");
                } else if (currentItem.channels > 1 || currentItem.imageColorSpace === ImageColorSpace.GrayScale) {
                    if (currentItem.colorants[0] === "Gray") {
                        if (colorsArray.toString().indexOf("Black") === -1) {
                            colorsArray.push("Black");
                        }
                        alert("When this script is finished, please verify that the Ink/PMS table has the correct colors.");
                    } else {
                        if (colorsArray.toString().indexOf(currentItem.colorants[0]) === -1) {
                            colorsArray.push(currentItem.colorants[0]);
                        }
                    }
                } else {
                    alert("The raster image in the art file must be a 1-channel bitmap and, thus, script cannot determine its color.");
                }
            } else if ((currentItem.fillColor || currentItem.strokeColor) && !currentItem.guides) {
                // If the current object has either a fill or a stroke, continue.
                if (currentItem.pathPoints.length > 2 || (currentItem.pathPoints == 2 && currentItem.stroked && currentItem..strokeWidth >= 0.1))
                {
                    // If the current object has 2 points and a good stroke, or more than two points, continue.
                    getColors(currentItem, colorsArray);
                }
            } else if (currentItem.typename === "CompoundPathItem") {
                for (var c = 0; c < currentItem.pathItems.length; c++) {
                    if (currentItem.pathItems[c].pathPoints.length > 2 || (currentItem.pathItems[c].pathPoints == 2 && currentItem.pathItems[c].stroked && currentItem.pathItems[c].strokeWidth >= 0.1))
                    {
                        // If the current object has 2 points and a good stroke, or more than two points, continue.
                        getColors(currentItem.pathItems[c], colorsArray);
                    }
                }
            }
        }
        return;
    }

    function getColors(currentItem, colorsArray)
    {
        try
        {
            var fillColorType = currentItem.fillColor.typename;
            var strokeColorType = currentItem.strokeColor.typename;
            $.writeln("fillColorType is " + fillColorType);
            switch (fillColorType) {
                case "CMYKColor":
                    if (currentItem.fillColor.cyan === 0 && currentItem.fillColor.magenta === 0 && currentItem.fillColor.yellow === 0) {
                        if (currentItem.fillColor.black > 0) {
                            if (colorsArray.toString().indexOf("Black") === -1) {
                                colorsArray.push("Black");
                            }
                            if (currentItem.fillColor.black < 100) {bHalftones = true;}
                        }
                    } else {
                        // $.writeln("Four color process!");
                        bFourCP = true;
                        bHalftones = true;
                    }
                    break;
                case "GrayColor":
                    if (currentItem.fillColor.gray > 0) {
                        if (colorsArray.toString().indexOf("Black") === -1) {
                            colorsArray.push("Black");
                        }
                        if (currentItem.fillColor.gray < 100) {bHalftones = true;}
                    }
                    break;
                case "SpotColor":
                    if (colorsArray.toString().indexOf(currentItem.fillColor.spot.name) === -1 && currentItem.fillColor.spot.name.toLowerCase().indexOf("white") === -1) {
                        colorsArray.push(currentItem.fillColor.spot.name);
                    }
                    if (currentItem.fillColor.tint < 100) {bHalftones = true;}
                    break;
                case "GradientColor":
                    bHalftones = true;
                    for (var j = 0; j < currentItem.fillColor.gradient.gradientStops.length; j++) {
                        var gStop = currentItem.fillColor.gradient.gradientStops[j].color;
                        switch (gStop.typename) {
                            case "GrayColor":
                                if (colorsArray.toString().indexOf("Black") === -1) {
                                    colorsArray.push("Black");
                                }
                                break;
                            case "SpotColor":
                                if (colorsArray.toString().indexOf(gStop.spot.name) === -1) {
                                    colorsArray.push(gStop.spot.name);
                                }
                                break;
                            case "CMYKColor":
                                if (gStop.cyan === 0 && gStop.magenta === 0 && gStop.yellow === 0 && gStop.black > 0) {
                                    if (colorsArray.toString().indexOf("Black") === -1) {
                                        colorsArray.push("Black");
                                    }
                                    if (gStop.black < 100) {bHalftones = true;}
                                } else if (gStop.cyan === 0 && gStop.magenta === 0 && gStop.yellow === 0 && gStop.black === 0) {
                                    break;
                                } else {
                                    // $.writeln("Four color process.");
                                    bFourCP = true;
                                    bHalftones = true;
                                }
                                break;
                            default:
                                // $.writeln("Four color process?");
                                bFourCP = true;
                                bHalftones = true;
                        }
                    }
                    break;
                case "NoColor":
                    break;
                default:
                    // $.writeln("The fill color on object number " + i + " is of type " + fillColorType);
            }

            switch (strokeColorType) {
                case "CMYKColor":
                    if (currentItem.strokeColor.cyan === 0 && currentItem.strokeColor.magenta === 0 && currentItem.strokeColor.yellow === 0) {
                        if (currentItem.strokeColor.black > 0) {
                            if (colorsArray.toString().indexOf("Black") === -1) {
                                colorsArray.push("Black");
                            }
                            if (currentItem.strokeColor.black < 100) {bHalftones = true;}
                        }
                    } else {
                        // $.writeln("Four color process!");
                        bFourCP = true;
                        bHalftones = true;
                    }
                    break;
                case "GrayColor":
                    if (currentItem.strokeColor.gray > 0) {
                        if (colorsArray.toString().indexOf("Black") === -1) {
                            colorsArray.push("Black");
                        }
                        if (currentItem.strokeColor.gray < 100) {bHalftones = true;}
                    }
                    break;
                case "SpotColor":
                    if (colorsArray.toString().indexOf(currentItem.strokeColor.spot.name) === -1) {
                        colorsArray.push(currentItem.strokeColor.spot.name);
                    }
                    if (currentItem.strokeColor.tint < 100) {bHalftones = true;}
                    break;
                case "GradientColor":
                    bHalftones = true;
                    for (var j = 0; j < currentItem.strokeColor.gradient.gradientStops.length; j++) {
                        var gStop = currentItem.strokeColor.gradient.gradientStops[j].color;
                        switch (gStop.typename) {
                            case "GrayColor":
                                if (colorsArray.toString().indexOf("Black") === -1) {
                                    colorsArray.push("Black");
                                }
                                break;
                            case "SpotColor":
                                if (colorsArray.toString().indexOf(gStop.spot.name) === -1) {
                                    colorsArray.push(gStop.spot.name);
                                }
                                break;
                            case "CMYKColor":
                                if (gStop.cyan === 0 && gStop.magenta === 0 && gStop.yellow === 0 && gStop.black > 0) {
                                    if (colorsArray.toString().indexOf("Black") === -1) {
                                        colorsArray.push("Black");
                                    }
                                    if (gStop.black < 100) {bHalftones = true;}
                                } else if (gStop.cyan === 0 && gStop.magenta === 0 && gStop.yellow === 0 && gStop.black === 0) {
                                    break;
                                } else {
                                    // $.writeln("Four color process.");
                                    bFourCP = true;
                                    bHalftones = true;
                                }
                                break;
                            default:
                                // $.writeln("Four color process?");
                                bFourCP = true;
                                bHalftones = true;
                        }
                    }
                    break;
                case "NoColor":
                    break;
                default:
                    // $.writeln("The stroke color on object number " + i + " is of type " + strokeColorType);
            }
        }
        catch (e) {/* If an error was found with the fill color and/or stroke color, then just skip this particular path item. */};
        return;
    }

    document.close(SaveOptions.DONOTSAVECHANGES);

    // Now we combine the gathered items into a single array and return it.
    if ((colorsArray.length > 1 && !/HI/.test(templateName.substring(0, 2))) || bFourCP) {bReg = true;}
    prodInfo.push(templateName, colorsArray, bHalftones, bFourCP, bReg);

    return prodInfo.toSource();
};

我知道它现在有点混乱,有许多$.writeln用于调试目的,其中一些甚至被注释掉,因为它们导致了错误。无论如何,我会非常感谢非常感谢您对此问题的任何帮助。

2 个答案:

答案 0 :(得分:2)

您的脚本无法查看所有pathItems,导致您的root是一个图层,如果您更改

colorsInUse(document.layers[0]);
to
colorsInUse(document);

您将看到所有pathItems,甚至包含在compoundPath

答案 1 :(得分:0)

通常,在Illustrator中甚至无法创建一组两个路径。

但是如果你创建一个一个路径的组,选择这个组和另一个路径,做一个这个选择的复合路径,你就会在一个复合路径中得到一个组。