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.
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,
...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.
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.
Copy link to clipboard
Copied
Nice one!
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") };