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

stroke to new layer

Explorer ,
Apr 13, 2023 Apr 13, 2023

I have a document with 3 shapes that each have a line with a different spot color:


1. Cut contour
2. Big
3. Pnimi

The script is like this:

I need the computer to recognize the spots
and will turn them into spot colors with the following values:


CutContour = R=255 G=0 B=0
Big = R=0 G=255 B=0
Pnimi = R=0 G=0 B=255


After that, will move all the lines to a new layer.

TOPICS
Scripting
2.2K
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 3 Correct answers

Community Expert , Apr 16, 2023 Apr 16, 2023

Here is a script that will do what you ask. I've made a function that works with CMYK as well as RGB colors. You can change the settings in the breakdowns object.

- Mark

 

/**
 * Change the color model and breakdown
 * of spot colors in active document.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/stroke-to-new-layer/m-p/13725064
 */
(function () {

    var breakdowns = {
        CutContour: [255, 0, 0],
        Big: [0, 255, 0],
        Pnimi: [0, 0, 255],
    };

    v
...
Translate
Community Expert , Apr 17, 2023 Apr 17, 2023

Add, the code below right after the line `alert('Changed ' + counter + ' spot colors.');` in @m1bs script.

 

A few caveats...

  1.  Iterating over all `pathItems` can be expensive in files with lots of paths. If you do have very complicated files with lots of paths, I would try selecting all of the shapes you are targeting first and then working with only the items in the `app.activeDocument.selection` array.
  2.  If your document has clipping masks, compound paths, or other funky pageItems, then your script
...
Translate
Community Expert , Apr 18, 2023 Apr 18, 2023

Here's the entire script and it works fine for me (see the screencast video).

Screen Shot 2023-04-18 at 12.50.15.gif

 

/**
 * Change the color model and breakdown
 * of spot colors in active document.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/stroke-to-new-layer/m-p/13725064
 */
(function () {
  var breakdowns = {
    CutContour: [255, 0, 0],
    Big: [0, 255, 0],
    Pnimi: [0, 0, 255],
  };

  var doc = app.activeDocument,
    counter = 0;

  // get the colors
  for (var swatchName in breakdowns) {
    try
...
Translate
Adobe
Community Expert ,
Apr 13, 2023 Apr 13, 2023

Please post example file. (Save as .pdf because forum software doesn't accept .ai files.)

- Mark

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
Explorer ,
Apr 13, 2023 Apr 13, 2023
 
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
Explorer ,
Apr 16, 2023 Apr 16, 2023

???

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 ,
Apr 16, 2023 Apr 16, 2023

95% of people here are helping in their spare time. Also: it's the weekend.

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 ,
Apr 16, 2023 Apr 16, 2023

Exactly Monika! Also we are trying to leave answers that can help future visitors, too. - Mark

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 ,
Apr 16, 2023 Apr 16, 2023

Here is a script that will do what you ask. I've made a function that works with CMYK as well as RGB colors. You can change the settings in the breakdowns object.

- Mark

 

/**
 * Change the color model and breakdown
 * of spot colors in active document.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/stroke-to-new-layer/m-p/13725064
 */
(function () {

    var breakdowns = {
        CutContour: [255, 0, 0],
        Big: [0, 255, 0],
        Pnimi: [0, 0, 255],
    };

    var doc = app.activeDocument,
        counter = 0;

    // get the colors
    for (var swatchName in breakdowns) {
        try {
            setSwatchSpotColor(doc.swatches.getByName(swatchName), breakdowns[swatchName]);
            counter++;
        } catch (error) { }
    }

    alert('Changed ' + counter + ' spot colors.');

})();


/**
 * Sets the color of a spot color swatch
 * based on the breakdown supplied.
 * @author m1b
 * @version 2023-04-17
 * @Param {Swatch} swatch - an Illustrator Swatch.
 * @Param {Array<Number>} breakdown - the color breakdown [R, G, B] or [C, M, Y, K].
 */
function setSwatchSpotColor(swatch, breakdown) {

    if (swatch.color.typename !== 'SpotColor')
        return;

    var newColor;

    if (breakdown.length === 3) {
        newColor = new RGBColor();
        newColor.red = breakdown[0];
        newColor.green = breakdown[1];
        newColor.blue = breakdown[2];
    }

    else if (breakdown.length === 4) {
        newColor = new CMYKColor();
        newColor.cyan = breakdown[0];
        newColor.magenta = breakdown[1];
        newColor.yellow = breakdown[2];
        newColor.black = breakdown[3];
    }

    swatch.color.spot.color = newColor;

};

 

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
Explorer ,
Apr 17, 2023 Apr 17, 2023
Excellent, but I want the colors to move to a new layer

Thanks
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 ,
Apr 17, 2023 Apr 17, 2023

Add, the code below right after the line `alert('Changed ' + counter + ' spot colors.');` in @m1bs script.

 

A few caveats...

  1.  Iterating over all `pathItems` can be expensive in files with lots of paths. If you do have very complicated files with lots of paths, I would try selecting all of the shapes you are targeting first and then working with only the items in the `app.activeDocument.selection` array.
  2.  If your document has clipping masks, compound paths, or other funky pageItems, then your script will have to get more complex to handle all of those situations. As it is now, it only works with simple pathItems.
  3.  Moving pathItems that are layered in a specific way or are part of some type of collection can have unintended consequences, so pay close attention to the before and after results.

 

  // make a new layer to hold the items
  layer = app.activeDocument.layers.add();
  layer.name = "MOVED HERE";

  // iterate over all path items and check if their stroke, if it matches
  // any of the items in 'breakdowns' then move that path to the new layer
  var item,
    counter = 0;
  for (var i = 0; i < doc.pathItems.length; i++) {
    item = doc.pathItems[i];
    if (
      item.stroked &&
      item.strokeColor.typename == "SpotColor" &&
      breakdowns.hasOwnProperty(item.strokeColor.spot.name)
    ) {
      item.move(layer, ElementPlacement.PLACEATEND);
      counter++;
    }
  }

  alert("Moved " + counter + " path items.");

 

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
Explorer ,
Apr 18, 2023 Apr 18, 2023

I have more question  

can you add in the end of script a command that delete all Colors from the Color list?

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 ,
Apr 18, 2023 Apr 18, 2023

Just add the code below at the end of the main code block right after the code I provided yesterday. It will delete any swatches not defined in the `breakdowns` object and will also clean up any empty swatch groups.

// remove all swatches not defined in breakdowns object
var swatch,
  counter = 0;
for (var i = doc.swatches.length - 1; i >= 0; i--) {
  swatch = doc.swatches[i];
  if (!breakdowns.hasOwnProperty(swatch.name)) {
    swatch.remove();
    counter++;
  }
}

alert("Deleted " + counter + " swatches.");

// remove any left over swatch groups
var swatchGroup,
  counter = 0;
for (var i = doc.swatchGroups.length - 1; i >= 0; i--) {
  swatchGroup = doc.swatchGroups[i];
  if (swatchGroup.getAllSwatches().length == 0) {
    swatchGroup.remove();
    counter++;
  }
}

alert("Deleted " + counter + " empty swatch groups.");

 

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
Explorer ,
Apr 18, 2023 Apr 18, 2023

Thanks but I need that the script deletes all swatches (the breakdown too)

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 ,
Apr 18, 2023 Apr 18, 2023

Then just change the first for loop in the above code to this... Note, this will still leave the "Registration" swatch.

counter = 0;
for (var i = doc.swatches.length - 1; i >= 0; i--) {
  doc.swatches[i].remove();
  counter++;
}

 

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
Explorer ,
Apr 18, 2023 Apr 18, 2023

here the code. it's not working:

 

// remove all swatches not defined in breakdowns object
var swatch,
counter = 0;
for (var i = doc.swatches.length - 1; i >= 0; i--) {
doc.swatches[i].remove();
counter++;
}

}
}

alert("Deleted " + counter + " swatches.");

// remove any left over swatch groups
var swatchGroup,
counter = 0;
for (var i = doc.swatchGroups.length - 1; i >= 0; i--) {
swatchGroup = doc.swatchGroups[i];
if (swatchGroup.getAllSwatches().length == 0) {
swatchGroup.remove();
counter++;
}
}

alert("Deleted " + counter + " empty swatch groups.");

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 ,
Apr 18, 2023 Apr 18, 2023
LATEST

Here's the entire script and it works fine for me (see the screencast video).

Screen Shot 2023-04-18 at 12.50.15.gif

 

/**
 * Change the color model and breakdown
 * of spot colors in active document.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/stroke-to-new-layer/m-p/13725064
 */
(function () {
  var breakdowns = {
    CutContour: [255, 0, 0],
    Big: [0, 255, 0],
    Pnimi: [0, 0, 255],
  };

  var doc = app.activeDocument,
    counter = 0;

  // get the colors
  for (var swatchName in breakdowns) {
    try {
      setSwatchSpotColor(doc.swatches.getByName(swatchName), breakdowns[swatchName]);
      counter++;
    } catch (error) {}
  }

  alert("Changed " + counter + " spot colors.");

  // make a new layer to hold the items
  layer = app.activeDocument.layers.add();
  layer.name = "MOVED HERE";

  // iterate over all path items and check if their stroke, if it matches
  // any of the items in 'breakdowns' then move that path to the new layer
  var item,
    counter = 0;
  for (var i = 0; i < doc.pathItems.length; i++) {
    item = doc.pathItems[i];
    if (
      item.stroked &&
      item.strokeColor.typename == "SpotColor" &&
      breakdowns.hasOwnProperty(item.strokeColor.spot.name)
    ) {
      item.move(layer, ElementPlacement.PLACEATEND);
      counter++;
    }
  }

  alert("Moved " + counter + " path items.");

  // remove all swatches not defined in breakdowns object
  counter = 0;
  for (var i = doc.swatches.length - 1; i >= 0; i--) {
    doc.swatches[i].remove();
    counter++;
  }

  alert("Deleted " + counter + " swatches.");

  // remove any left over swatch groups
  var swatchGroup,
    counter = 0;
  for (var i = doc.swatchGroups.length - 1; i >= 0; i--) {
    swatchGroup = doc.swatchGroups[i];
    if (swatchGroup.getAllSwatches().length == 0) {
      swatchGroup.remove();
      counter++;
    }
  }

  alert("Deleted " + counter + " empty swatch groups.");
})();

/**
 * Sets the color of a spot color swatch
 * based on the breakdown supplied.
 * @author m1b
 * @version 2023-04-17
 * @Param {Swatch} swatch - an Illustrator Swatch.
 * @Param {Array<Number>} breakdown - the color breakdown [R, G, B] or [C, M, Y, K].
 */
function setSwatchSpotColor(swatch, breakdown) {
  if (swatch.color.typename !== "SpotColor") return;

  var newColor;

  if (breakdown.length === 3) {
    newColor = new RGBColor();
    newColor.red = breakdown[0];
    newColor.green = breakdown[1];
    newColor.blue = breakdown[2];
  } else if (breakdown.length === 4) {
    newColor = new CMYKColor();
    newColor.cyan = breakdown[0];
    newColor.magenta = breakdown[1];
    newColor.yellow = breakdown[2];
    newColor.black = breakdown[3];
  }

  swatch.color.spot.color = newColor;
}

 

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