Skip to main content
Inspiring
August 4, 2022
Answered

Illustrator Scripting - Combining/Merging multiple CompoundPathItem into one

  • August 4, 2022
  • 3 replies
  • 487 views

Hey guys,

 

I would like to combine/merge multiple CompoundPathItem into a single one.

 

Has anyone done something similar previously?

 

My code so far looks like this:

 

var flattened = doc.activeLayer.compoundPathItems.add();
for (var i = 0; i < compoundPaths.length; i++) {
    var currCompoundPath = compoundPaths[i];
    var currPathItems = currCompoundPath.pathItems;
    
    for (var j = 0; j < currPathItems.length; j++) {
        var currPathItem = currPathItems[j];
        flattened.pathItems.add(currPathItem);
    }
}

 

 But apart from copying over the pathItems that a CompoundPathItem contains it doesn't do much more.

 

I think I am missing something, any help would be appreciated!

This topic has been closed for replies.
Correct answer JSuX

The solution for this is:

app.executeMenuCommand("noCompoundPath");
app.executeMenuCommand("compoundPath");
app.executeMenuCommand("group");

 

.. executed on all selected CompoundPathItem

3 replies

renél80416020
Inspiring
August 5, 2022

Bonjour Ivan,

En effet votre code ne fait pas grand chose...

Je parle du premier code.

Voici une solution, testée sur les versions 11, 12, 15 antérieures à la version 16 (CS6):

 

 

 

var doc = activeDocument;
var lay = doc.activeLayer;
var compoundPaths = lay.compoundPathItems;
if (compoundPaths.length > 1) {
  var flattened = lay.compoundPathItems.add();
  var currCompoundPath, Paths, newPath, fcolor, sColor;
      for (var i = 0; i < compoundPaths.length; i++) {
        currCompoundPath = compoundPaths[i];
        Paths = currCompoundPath.pathItems;
          for (var j = Paths.length-1; j >= 0; j--) {
            if (i == compoundPaths.length-1) {
              fcolor = Paths[j].fillColor;
              scolor = Paths[j].strokeColor;
            }
            newPath = flattened.pathItems.add();
            Paths[j].duplicate(newPath);
          }
      }
    flattened.pathItems[0].fillColor = fcolor;
    flattened.pathItems[0].strokelColor = scolor;
    for (var i = compoundPaths.length-1; i >= 1; i--) {
      compoundPaths[i].remove();
    }
}

 

 

 

Peut on savoir pourquoi vous avez besoin d'un tel script?

René

PS pour executeMenuCommand

app.executeMenuCommand("compoundPath");

cette ligne doit suffire

 

 

JSuXAuthorCorrect answer
Inspiring
August 4, 2022

The solution for this is:

app.executeMenuCommand("noCompoundPath");
app.executeMenuCommand("compoundPath");
app.executeMenuCommand("group");

 

.. executed on all selected CompoundPathItem

Charu Rajput
Community Expert
Community Expert
August 4, 2022

@JSuX 

You want to release all compoundPath Items?

Best regards