Highlighted

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

New Here ,
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

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.

TOPICS
Scripting

Views

58

Likes

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

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

New Here ,
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

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.

TOPICS
Scripting

Views

59

Likes

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

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 31, 2020 2
New Here ,
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

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 31, 2020 0
Contributor ,
Oct 31, 2020

Copy link to clipboard

Copied

Nice one!

Likes

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
Reply
Loading...
Oct 31, 2020 0
Most Valuable Participant ,
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

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 01, 2020 1
Resources