Skip to main content
Inspiring
February 14, 2023
Answered

Script to Remove extra Paths within a Compound Path?

  • February 14, 2023
  • 2 replies
  • 4142 views

Hey Everyone,

I've been trying to write a script to remove the inside paths leaving the needed ones behind, but I can't get it quite right...

// Get the active document and selection
var doc = app.activeDocument;
var sel = doc.selection;

// Loop through the selection
for (var i = 0; i < sel.length; i++) {
  var item = sel[i];
  // Check if the item is a compound path  
  if (item.typename === 'CompoundPathItem') {

    // Get the paths in the compound path    
    var paths = item.pathItems;

    // Check the number of paths in the compound path    
    if (paths.length > 2) {
      // If there are more than 2 paths, delete all but the outside and inside most paths      
      var numPaths = paths.length;
      var firstPath = paths[0];
      var lastPath = paths[numPaths - 1];
      var secondPath = paths[1];
      var secondLastPath = paths[numPaths - 2];
      
      if (numPaths === 3) {
        // If there are only 3 paths, delete the middle path        
        secondPath.remove();
      } else {
        // If there are more than 3 paths, delete all but the outside and inside most paths        
        for (var j = numPaths - 2; j > 1; j--) {
          secondPath.remove();
          lastPath.remove();
        }
      }
    }
  }
}
This topic has been closed for replies.
Correct answer CarlosCanto

@femkeblanco I believe you are correct.
Here is the PDF version


it seems like the lines are multiples of 3. Holes are on top, outside lines are at the bottom

 

- if count == 3, keep 2

- if count == 6, keep 0, 5

- if count == 9, keep 0, 3, 8

 

that should cover 0, 1 and 2 holes, it doesn't account for characters like i 

2 replies

m1b
Community Expert
Community Expert
February 14, 2023

@BryanPagenkopf I'm a bit stumped by this one. It is harder than you might think. I would love to see someone solve it.

 

If you need to resort to manual methods, then PathFinder Unite does a good job immediately. But the PathFinder options we have available via the scripting API don't seem to do what the normal PathFinder does in this case. The project is complicated by the stroke aligned with outside path perhaps.

- Mark

femkeblanco
Legend
February 14, 2023
var doc = app.activeDocument;
var sel = doc.selection;
for (var i = 0; i < sel.length; i++) {
    var item = sel[i];
    if (item.typename === 'CompoundPathItem') {
        var paths = item.pathItems;
        var numPaths = paths.length;
        if (numPaths > 2) {
        // If there are >2 paths, 
        // delete all but the outside and inside most paths
            for (var j = numPaths - 2; j > 0; j--) {
                paths[j].remove();
            }
        }
    }
}
Inspiring
February 14, 2023

I'm not sure why but I am getting mixed results with your script @femkeblanco 


The 2nd "A" turned out how it should 🙂

 

femkeblanco
Legend
February 14, 2023

Unfortunately, I can't open your file (because of my version of AI), but I presume the result you get is because of the anatomy of the items.  For example, the path item that makes the "hole" in the A may be the topmost path item in the second A, but not the first A.