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

Script to Remove extra Paths within a Compound Path?

Engaged ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

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();
        }
      }
    }
  }
}
TOPICS
Scripting

Views

1.7K

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

Community Expert , Feb 14, 2023 Feb 14, 2023

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 

Votes

Translate

Translate
Guide ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

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();
            }
        }
    }
}

Votes

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
Engaged ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

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

BryanPagenkopf_0-1676401500544.png


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

 

Votes

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
Guide ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

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. 

Votes

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
Community Expert ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

Hi @BryanPagenkopf, you can post ai files if they are saved as pdf with illustrator editing capabilities. 

 

In the case with the text outlines there may be challenges in working out which paths to remove. I'm wondering if using pathFinder could help here. - Mark

Votes

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
Engaged ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

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

Votes

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
Community Expert ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

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 

Votes

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
Community Expert ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

OMG great work @CarlosCanto!

var p = item.pathItems.length;

if (p == 3) {
    //  if count == 3, keep 2
    item.pathItems[1].remove();
    item.pathItems[0].remove();
}
else if (p == 6) {
    // - if count == 6, keep 0, 5
    item.pathItems[4].remove();
    item.pathItems[3].remove();
    item.pathItems[2].remove();
    item.pathItems[1].remove();
}
else if (p == 9) {
    // - if count == 9, keep 0, 3, 8
    item.pathItems[7].remove();
    item.pathItems[6].remove();
    item.pathItems[5].remove();
    item.pathItems[4].remove();
    item.pathItems[2].remove();
    item.pathItems[1].remove();
}

@BryanPagenkopf, see how you go with Carlos' advice. In my test with your sample file, it worked on all except the first A which is malformed (it has 7 sub paths due to a glitchy bit near the top). 

- Mark

Votes

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
Engaged ,
Feb 15, 2023 Feb 15, 2023

Copy link to clipboard

Copied

Thank you! 
I'll have to play around with "i" "j" ":" ";"   characters

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 p = item.pathItems.length;

if (p == 3) {
    //  if count == 3, keep 2
    item.pathItems[1].remove();
    item.pathItems[0].remove();
}
else if (p == 6) {
    // - if count == 6, keep 0, 5
    item.pathItems[4].remove();
    item.pathItems[3].remove();
    item.pathItems[2].remove();
    item.pathItems[1].remove();
}
else if (p == 9) {
    // - if count == 9, keep 0, 3, 8
    item.pathItems[7].remove();
    item.pathItems[6].remove();
    item.pathItems[5].remove();
    item.pathItems[4].remove();
    item.pathItems[2].remove();
    item.pathItems[1].remove();
}
    }
}

 

Votes

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
Community Expert ,
Feb 15, 2023 Feb 15, 2023

Copy link to clipboard

Copied

quoteI'll have to play around with "i" "j" ":" ";"   characters

 

post samples to analyze

Votes

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
Engaged ,
Feb 15, 2023 Feb 15, 2023

Copy link to clipboard

Copied

I ran this code and it fixed all of the characters except the "?"

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 p = item.pathItems.length;

        if (p == 3) {
            //  if count == 3, keep 2
            item.pathItems[1].remove();
            item.pathItems[0].remove();
        }
        else if (p == 6) {
            // - if count == 6, keep 0, 5
            item.pathItems[4].remove();
            item.pathItems[3].remove();
            item.pathItems[2].remove();
            item.pathItems[1].remove();
        }
        else if (p == 9) {
            // - if count == 9, keep 0, 3, 8
            item.pathItems[7].remove();
            item.pathItems[6].remove();
            item.pathItems[5].remove();
            item.pathItems[4].remove();
            item.pathItems[2].remove();
            item.pathItems[1].remove();
        }
    }
}

Votes

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
Community Expert ,
Feb 15, 2023 Feb 15, 2023

Copy link to clipboard

Copied

@BryanPagenkopf  I had a look at the reference file, and here's what I noticed: the questionmark's paths are ordered differently to the others (one of the top bit of the question mark's paths are mixed in with the dots path order.) You can fix this by just selecting the 3 dot paths and bringing to front. If you need to automate this process (including analysing when it occurs, we would need much more samples to test).

 

Although, who knows? again Carlos may just have the answer! 🙂

- Mark

Votes

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
Community Expert ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

something is wrong with that file, like you said, the dot's outer path of the Question mark is not part of the compound path. But all other 5 paths follow the same order as the original characters so the script should work with the question mark as well.

Votes

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
Community Expert ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

LATEST

You're right Carlos, I forgot that I had fixed that first. Anyway, the point is that these are not all normal, untouched, "create outlines" paths. For example, the two dots of the colon are not in the same compoundPathItem, which they would be after create outlines. In that case it is fine, because the path order is still in the default order.

 

So @BryanPagenkopf are you creating these outines? If so, please be careful what you do to them after creating outlines, because Carlos' script will only work if they remain in the default order.

Votes

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
Community Expert ,
Feb 14, 2023 Feb 14, 2023

Copy link to clipboard

Copied

@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

Votes

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