3

我在使用 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 用于调试目的,其中一些甚至被注释掉了,因为它们会导致错误。无论如何,我将非常感谢有关此问题的任何帮助。

4

2 回答 2

2

你的脚本看不到所有 pathItems 因为你的根是一层,如果你改变

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

您会看到所有 pathItems,甚至包括在 complexPath 中

于 2014-10-23T11:05:11.123 回答
0

通常,在 Illustrator 中甚至不可能创建一组两条路径。

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

于 2014-11-10T16:03:57.150 回答