script to select path based on spot color

New Here ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

Hello, i'm new in scripting. I want my script to select paths based on the spot color named "CutContour"

I copied it on similar scripts i found in this community, but it doesn't work.

here's the script :

Edouard76_0-1655281125163.png

I keep getting this error "undefined is not an object" on line 10 and can't find any solution.

Can somebody help me, please ?

 

TOPICS
Scripting

Views

150

Likes

Translate

Translate

Report

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

Adobe Community Professional , Jun 15, 2022 Jun 15, 2022
so are there multiple cutcontour shapes, some of which are not located on the "cutcontour" layer? could you just select the artwork on the cutcontour layer and call it a day? If not, you'll likely need a recursive approach, because if the cutcontour lines you're looking for are nested inside of groups (potentially deeply nested), the only way to find them is to search each nested group to see if it contains any cutcontour paths. Another possible option is to set your selection to null, then ...

Likes

Translate

Translate
Advisor ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

Change line 9 to

var ipath = objects[i];

Likes

Translate

Translate

Report

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 ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

thanks for the reply. It works but there's another path in a 2nd layer also named "CutContour" that hasn't been selected in my test file.

and i still get the error after

Edouard76_0-1655287170899.png

All files (7000 !) I have to upgrade are made like this one. 

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

so are there multiple cutcontour shapes, some of which are not located on the "cutcontour" layer?

could you just select the artwork on the cutcontour layer and call it a day?

 

If not, you'll likely need a recursive approach, because if the cutcontour lines you're looking for are nested inside of groups (potentially deeply nested), the only way to find them is to search each nested group to see if it contains any cutcontour paths.

 

Another possible option is to set your selection to null, then set the "default stroke color" to cut contour, then use executeMenuCommand to select all same strokes. that should select all of the cut lines for you (but depending on what you want to do with them later, selecting them this way is not always the best choice). 

 

 

 

mydoc.selection = null;
mydoc.defaultStrokeColor = mydoc.swatches["CutContour"].color;
app.executeMenuCommand("Find Stroke Color menu item");

 

 

 

 

Likes

Translate

Translate

Report

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 ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

Hi, and thank you !

CutContour shapes are not supposed to be in the "impression" layer, only in the "CutContour" layer, but I know by experience that I'm going to find a lot of them there in the 7000+ files I have to deal with, some of them potentially "nested" in groups. A lot of these files are not made the right way (our way). That's why I draw a few more cutcontour shapes in my test file.

The final idea of the script is to open a file, find all CutContour shapes in the file, change their stroke color to none, remove them out of the CutContour layer (not delete them), delete the empty CutContour Layer, save the file in  new folder and close the file. 

Like you said, there's certainly no need to select them (I learned this while searching a solution to my problem).

Do you think this is possible ? I'm no asking you to write the whole thing for me. I'd like to do it myself and try to learn the most of it.

 

By the way, your code works like a charm !!

I'm sorry I won't be available before next monday.

Thank you!!

Likes

Translate

Translate

Report

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
Advocate ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

Bonjour,

 

  obj = selection[0]
  color = obj.fillColor;
    if (color.typename == "SpotColor") {
      if (color.spot.name == "CutContour") alert("CutContour");
    }

 

Je pense que cela peut convenir.

REné

 

Likes

Translate

Translate

Report

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 ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

Bonjour et merci de votre réponse. Malheureusement je ne sais pas comment integrer votre code dans mon script...

dans la boucle ? obj et color sont-elles des variables? j'ai aussi peut-être oublié de préciser dans mon descriptif qu'il s'agit de la couleur de contour (strokeColor) et non celle de fond (fillColor). 

Likes

Translate

Translate

Report

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
Advocate ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

Désolé je n'ai pas fait attention...

// JavaScript Document for Illustrator
var mydoc = app.activeDocument; //app.open.....
    app.selection = null;
function test(relativeObjet) {
  var objets = relativeObjet.pathItems;
    for (var i = 0; i < objets.length; i++) {
      var ipath = objets[i];
      var color = ipath.strokeColor;
        if (color.typename == "SpotColor") {
          if (color.spot.name == "CutContour") {
            ipath.selected = true;
          }
        }
    }
}
test (mydoc);

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

Solid logic, but this will only find top level paths and it will ignore any thing that's nested or grouped.

 

Ninja edit.. I just read it again, and I'm wrong. Your code will access nested items.. But it does so in a very inefficient way. When you access a collection (pathItems, groupItems, etc) at the document level, you get EVERY item, including anything that's in a compound path and other such containers). It doesn't take very much for this process to get totally bogged down and take ages to finish processing. 

Likes

Translate

Translate

Report

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
Advocate ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

Merci pour vos remarques DilliamWowling,

Pour ma part, j'ai seulement voulu calquer et corriger le script proposé...

Votre solution ce qu'il y a de mieux.

René

PS Que proposez vous alors pour les versions antérieures à CS6 ?

Likes

Translate

Translate

Report

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 ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

In versions older than CS6, you can analyze layers and their pageItems.

selection = null;
process(activeDocument.layers);

function process(layers) {
  for (var i = 0; i < layers.length; i++) {
    var cur = layers[i];
    if (cur.visible && !cur.locked) {
      checkStrokeColor(cur.pageItems);
      if (cur.layers.length) process(cur.layers);
    }
    
  }
}

function checkStrokeColor(items) {
  var arr = getPaths(items);
  for (var i = 0; i < arr.length; i++) {
    var cur = arr[i];
    var color = cur.strokeColor;
    if (/spot/i.test(color.typename) && color.spot.name == "CutContour") {
      if (/compound/i.test(cur.parent.typename)) cur.parent.selected = true;
      else cur.selected = true;
    }
  }
}

function getPaths(items) {
  var out = [];
  for (var i = 0; i < items.length; i++) {
    var cur = items[i];
    if (cur.pageItems && cur.pageItems.length) {
      out = [].concat(out, getPaths(cur.pageItems));
    } else if (/compound/i.test(cur.typename) && cur.pathItems.length) {
      if (cur.pathItems[0].editable) out.push(cur.pathItems[0]);
    } else if (/pathitem/i.test(cur.typename)) {
      if (cur.editable) out.push(cur);
    }
  }
  return out;
}

 

Likes

Translate

Translate

Report

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
Advocate ,
Jun 17, 2022 Jun 17, 2022

Copy link to clipboard

Copied

Conclusion: avec les anciennes versions on n'avait pas le choix.

Que nous apporte la propriété editable ?

Je n'ai jamais compris sa signification.

René

Likes

Translate

Translate

Report

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 ,
Jun 17, 2022 Jun 17, 2022

Copy link to clipboard

Copied

if (obj.editable == true) is a shorter way of checking than writing if (obj.hidden == false && obj.locked == false)

Likes

Translate

Translate

Report

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
Advocate ,
Jun 17, 2022 Jun 17, 2022

Copy link to clipboard

Copied

LATEST

Merci Sergey Osokin, je ne manquerai pas de l'utiliser désormais.

Likes

Translate

Translate

Report

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