Illustrator在复合路径内分组对象

问题描述 投票:3回答:2

我正在使用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();
};

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

javascript adobe-illustrator extendscript
2个回答
2
投票

如果更改,则您的脚本无法查看所有pathItems因为您的根是图层

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

你会看到所有的pathItems,甚至包含在一个复合路径中


0
投票

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

但是,如果您创建一个包含一个路径的组,请选择此组和另一个路径,创建此选择的复合路径,您将在复合路径中获得一个组。

© www.soinside.com 2019 - 2024. All rights reserved.