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

123

Likes

translate

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

Engaged , 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,...

Likes

translate

Translate

Translate
Engaged ,
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.

Likes

translate

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.

Likes

translate

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

Copy link to clipboard

Copied

Nice one!

Likes

translate

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

Copy link to clipboard

Copied

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

 

 

Likes

translate

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