gpt4 book ai didi

javascript - Illustrator 在复合路径内对对象进行分组

转载 作者:行者123 更新时间:2023-11-29 14:54:57 26 4
gpt4 key购买 nike

我在使用 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,其中一些甚至被注释掉了,因为它们会导致错误。无论如何,如果您对此问题提供任何帮助,我将非常感激不尽。

最佳答案

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

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

你会看到所有的 pathItems,甚至包括在一个 compoundPath 中

关于javascript - Illustrator 在复合路径内对对象进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19458141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com