Copy link to clipboard
Copied
#target illustrator
// 获取当前选中的对象
var selectedObject = app.activeDocument.selection[0];
// 检查是否选择了对象
if (selectedObject == null) {
alert("请选择一个对象!");
} else {
// 获取对象的填充颜色
var fillColor = selectedObject.fillColor;
// 检查对象是否有填充色
if (fillColor.typename == "NoColor") {
alert("所选对象没有填充色!");
} else {
// 1. 选中相同填充色的对象
app.activeDocument.selection = null; // 清空当前选择
for (var i = 0; i < app.activeDocument.pageItems.length; i++) {
var pageItem = app.activeDocument.pageItems[i];
if (pageItem.fillColor.typename !== "NoColor" &&
pageItem.fillColor.cyan === fillColor.cyan &&
pageItem.fillColor.magenta === fillColor.magenta &&
pageItem.fillColor.yellow === fillColor.yellow &&
pageItem.fillColor.black === fillColor.black) {
pageItem.selected = true;
}
}
// 2. 获取 CMYK 颜色值
var c = Math.round(fillColor.cyan * 100);
var m = Math.round(fillColor.magenta * 100);
var y = Math.round(fillColor.yellow * 100);
var k = Math.round(fillColor.black * 100);
// 3. 使用 CMYK 值作为专色名称
var spotColorName = "C" + c + "M" + m + "Y" + y + "K" + k;
// 4. 创建新的专色
var newSpotColor = app.activeDocument.swatches.add();
// 先设置颜色类型为专色
newSpotColor.colorType = ColorModel.SPOT;
// 再设置颜色值
newSpotColor.color = fillColor;
newSpotColor.name = spotColorName;
// 将信息输出到控制台
$.writeln("已创建专色: " + newSpotColor.name);
}
}
Change #4 to the following... Learn more here.
// 4. 创建新的专色
var newSpotColor = app.activeDocument.spots.add();
Copy link to clipboard
Copied
Change #4 to the following... Learn more here.
// 4. 创建新的专色
var newSpotColor = app.activeDocument.spots.add();
Copy link to clipboard
Copied
Hello jduncan, your modifications have been very effective, thank you so much!! However, I have encountered a new issue: after the script selects all objects with the same fill color and defines them as spot colors, these objects do not apply the defined spot color and remain in CMYK color. Can you tell me why this is happening?
Copy link to clipboard
Copied
There are a few reasons why your script wasn't working as you intended. Instead of explaining each, I have included a new script that should do what I think you are trying to do. Hopefully, my comments can help you understand what is happening.
#target illustrator
(function () {
// no need to continue if there is no active document
if (!app.documents.length) {
alert("No active document.");
return;
}
var doc = app.activeDocument;
// no need to continue if there is no active selection
if (!doc.selection.length) {
alert("No active selection.");
return;
}
var sel = doc.selection;
var targetObject = sel[0];
var targetFillColor = targetObject.fillColor;
if (targetFillColor.typename == "NoColor") {
alert("所选对象没有填充色!");
return;
}
// format the name for your spot color
var c = Math.round(targetFillColor.cyan * 100);
var m = Math.round(targetFillColor.magenta * 100);
var y = Math.round(targetFillColor.yellow * 100);
var k = Math.round(targetFillColor.black * 100);
var spotColorName = "C" + c + "M" + m + "Y" + y + "K" + k;
// setup your spot color
var newSpot = doc.spots.add();
newSpot.colorType = ColorModel.SPOT;
newSpot.color = targetFillColor;
newSpot.name = spotColorName;
$.writeln("已创建专色: " + newSpot.name);
// create a new spot color object that can be applied to your page items
var newSpotColor = new SpotColor();
newSpotColor.spot = newSpot;
// iterate over the page items and change the fill of any that match the targetFillColor
// ** please note this will only work for very basic art because page items have many different types which can cause issues **
var pageItems = doc.pageItems;
var pageItem;
for (var i = 0; i < pageItems.length; i++) {
pageItem = pageItems[i];
if (pageItem.fillColor.typename == "NoColor") {
continue;
}
if (
pageItem.fillColor.cyan === targetFillColor.cyan &&
pageItem.fillColor.magenta === targetFillColor.magenta &&
pageItem.fillColor.yellow === targetFillColor.yellow &&
pageItem.fillColor.black === targetFillColor.black
) {
pageItem.fillColor = newSpotColor;
}
}
})();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now