Skip to main content
Participating Frequently
October 1, 2020
Answered

Issue duplicating compound path item on new layer

  • October 1, 2020
  • 4 replies
  • 1726 views

Hi, 

 

I have a script to auto create different colors of a sign. The first part runs fine. But, I then need to copy the path items to a new file/layer that uses an image for the background. When I run my script, it works great until it hits compound path items. When it does, it's filling in the enclosed text characters (see attached). 

 

Here is the code I'm using for the copy routine:

 

// Open the appropriate BMS file to process
var bmsFile = File.openDialog('Select the Brushed Stainless base image to use...');
if(bmsFile != null) {app.open(bmsFile);};

var srcDoc = app.documents[1];
var targetDoc = app.documents[0].layers.add();
var _sel = app.activeDocument.selection[0];

for (i=0; i<srcDoc.pathItems.length-1; i++) {
srcDoc.pathItems[i].duplicate(targetDoc, ElementPlacement.PLACEATBEGINNING);
}

 

I'm using Illustrator 2020

 

Thanks in advance for any help

This topic has been closed for replies.
Correct answer Silly-V

Oh, duh, this could only work when targeting a specific layer.

Assuming your document has one layer, or the art is on the 1st layer,

for (i = 0;  i < srcDoc.layers[0].pageItems.length - 1; i++) {
  if (srcDoc.layers[0].pageItems[i].typename == "PathItem" || srcDoc.layers[0].pageItems[i].typename == "CompoundPathItem") {
    srcDoc.layers[0].pageItems[i].duplicate(targetDoc, ElementPlacement.PLACEATBEGINNING);
  }
}

4 replies

Silly-V
Legend
October 2, 2020

Change this:

for (i=0; i<srcDoc.pathItems.length-1; i++) {
srcDoc.pathItems[i].duplicate(targetDoc, ElementPlacement.PLACEATBEGINNING);
}

 

To this:

for (i = 0;  i < srcDoc.pageItems.length - 1; i++) {
  if (srcDoc.pageItems[i].typename == "PathItem" || srcDoc.pageItems[i].typename == "CompoundPathItem") {
    srcDoc.pageItems[i].duplicate(targetDoc, ElementPlacement.PLACEATBEGINNING);
  }
}
femkeblanco
Legend
October 2, 2020

Silly-V

 

When I run your snippet, the copied compound paths are released. 

renél80416020
Inspiring
October 2, 2020

Salut!

Peut-être tout simplement:

Si les éléments à dupliquer sont contenus dans un groupe, sélectionnez ce groupe et lancer la sript qui suit.

// JavaScript Document for Illustrator
var srcDoc = app.activeDocument;
var _sel = selection[0]; // alert(_sel)
var bmsFile = File.openDialog();
var targetDoc = app.open(bmsFile);
var targetLayer = targetDoc.layers.add();
  _ sel.duplicate(targetLayer, ElementPlacement.PLACEATBEGINNING);

 

CarlosCanto
Community Expert
Community Expert
October 2, 2020

if you provide a sample file it could help speed things up.

femkeblanco
Legend
October 1, 2020

The last three lines would copy the pathItems in compoundPathItems, not the compoundPathItems themselves.  To copy the compoundPathItems themselves:

for (var i = 0; i < srcDoc.compoundPathItems.length; i++) {
  srcDoc.compoundPathItems[i].duplicate(targetDoc, ElementPlacement.PLACEATBEGINNING);
}

 

Participating Frequently
October 1, 2020

Thanks femkeblanco, 

 

Definitely headed in the right direction. I replaced the pathItem lines with your code and got the 1st image. Soooo, how do I get both the pathItems and the compoundPathItems. If I use my code and yours I still get the 2nd image. I'm guessing there needs some sort of typename check but am unclear how to do that. 

femkeblanco
Legend
October 1, 2020

Big thanks femkeblanco. That loop caught the different item types, but when it got to the 2nd for loop the script changed to the previous layer (which is white) and added the compound path items there. It didn't add the compound items to the new brushed stainless layer that the path items had been added. 

 

No idea how it could go back to the previous layer when targetDoc is the new layer.

Thanks again. 


Assuming you're running the script while in the last created document, and replacing the whole code in your original post with the below, what happens? 

var file1 = File.openDialog();
var pic1 = activeDocument.placedItems.add();
pic1.file = file1;
var srcDoc = app.documents[1];
var targetDoc = app.documents[0].layers.add();
var _sel = app.activeDocument.selection[0];
for (var i = 0; i < srcDoc.pathItems.length; i++) {
  if (srcDoc.pathItems[i].parent.typename == "CompoundPathItem") {
  continue;}
  srcDoc.pathItems[i].duplicate(targetDoc, ElementPlacement.PLACEATBEGINNING);
}
for (var i = 0; i < srcDoc.compoundPathItems.length; i++) {
  srcDoc.compoundPathItems[i].duplicate(targetDoc, ElementPlacement.PLACEATBEGINNING);
}