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

Help with removing "<Compound Path>" from illustrator with scripts.

New Here ,
Oct 31, 2020 Oct 31, 2020

Copy link to clipboard

Copied

Hello,

 

I am writing out a script where I am trying to delete a comound path from my first layer. I have spent some time already on and off this problem for the past 5 hours. Some document directions, expanations, and or example code is appriciated. Below I will attach what my layer looks like.

 

Brian5F93_0-1604187270517.png

 

Much love to this community,

Brian Almaguer

TOPICS
Scripting

Views

521

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 , Oct 31, 2020 Oct 31, 2020

Hi Brian,

 

Does this help:

 

var items = app.activeDocument.layers.getByName('Layer 1').compoundPathItems;
for (var i = items.length - 1; i ==0 ; i--) {
    items[i].remove();
}

 

 

If you want to target the first layer, change the first line to:

 

var items = app.activeDocument.layers[0].compoundPathItems;

 

 

Edit: by the way, I ran the for loop backwards deliberately. Deleting items from an array-like object, eg. [CompoundPathItems], seems to invalidate the array indices, so if you delete the first one,

...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 31, 2020 Oct 31, 2020

Copy link to clipboard

Copied

Hi Brian,

 

Does this help:

 

var items = app.activeDocument.layers.getByName('Layer 1').compoundPathItems;
for (var i = items.length - 1; i ==0 ; i--) {
    items[i].remove();
}

 

 

If you want to target the first layer, change the first line to:

 

var items = app.activeDocument.layers[0].compoundPathItems;

 

 

Edit: by the way, I ran the for loop backwards deliberately. Deleting items from an array-like object, eg. [CompoundPathItems], seems to invalidate the array indices, so if you delete the first one, the second one won't be accessible in the second place anymore.

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
New Here ,
Oct 31, 2020 Oct 31, 2020

Copy link to clipboard

Copied

This works perfectly. I also tried rewriting for my self to make this more readable for my self and this is what I came up with:

var doc = app.activeDocument;
doc.activeLayer = doc.layers.getByName('Layer 1');
layer = doc.activeLayer;
layer.compoundPathItems[0].remove();

 I'll refine it later but thanks for your help! I am reading up on the compoundPathItems documentation right now.

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 ,
Oct 31, 2020 Oct 31, 2020

Copy link to clipboard

Copied

Nice one!

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 ,
Nov 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

LATEST

Hi @Brian5F93 

your rewritten code can be shortened as:

 

app.activeDocument.layers.getByName('Layer 1').compoundPathItems[0].remove();

 

 

It can be useful to add a try-catch clause if your level 'Layer 1' does not exist or does not contain a compound path element (keyword: error management).

for example:

 

try {
app.activeDocument.layers.getByName('Layer 1').compoundPathItems[0].remove();
}
catch (e) { alert ("nothing happened") };

 

 

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