Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How should I modify this script to define colors as spot colors?

New Here ,
Sep 02, 2024 Sep 02, 2024
#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);
  }
}
TOPICS
Scripting , Third party plugins , Tools
405
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 03, 2024 Sep 03, 2024

Change #4 to the following... Learn more here.

// 4. 创建新的专色
    var newSpotColor = app.activeDocument.spots.add();

 

Translate
Adobe
Community Expert ,
Sep 03, 2024 Sep 03, 2024

Change #4 to the following... Learn more here.

// 4. 创建新的专色
    var newSpotColor = app.activeDocument.spots.add();

 

dad x 2. designer. maker. fun haver. ‍
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 03, 2024 Sep 03, 2024

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 03, 2024 Sep 03, 2024
LATEST

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.

 

Please note, this will only work for very basic art because page items have many different types which can cause issues.
 
Let me know if you have any questions.

 

#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;
    }
  }
})();

 

dad x 2. designer. maker. fun haver. ‍
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines