Skip to main content
Inspiring
October 26, 2024
Answered

deleting all matching items

  • October 26, 2024
  • 1 reply
  • 290 views

There are four rectangles in the document. All of them have no fill AND no stroke. When I try to delete them all based on this condition, I get that only two of them are deleted. I expected all of them to be deleted. What is the problem?

 

 

 

 

var doc = app.activeDocument;
processItems(doc.pageItems);

function hasNoFillAndStroke(item) {
    return !item.filled && !item.stroked;
}

function processItems(items) {
    for (var i = 0; i < items.length; i++) {
        var item = items[i];

        if (hasNoFillAndStroke(item)) {
            alert(item.typename);
            item.remove();
        }
    }
}

 

 

 

This topic has been closed for replies.
Correct answer Charu Rajput

Hi @andyf65867865 ,

Use the loop in the reverse direction.

 

var doc = app.activeDocument;
processItems(doc.pageItems);

function hasNoFillAndStroke(item) {
    return !item.filled && !item.stroked;
}

function processItems(items) {
    for (var i = items.length - 1; i >= 0; i--) {
        var item = items[i];

        if (hasNoFillAndStroke(item)) {
            alert(item.typename);
            item.remove();
        }
    }
}

 

1 reply

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
October 26, 2024

Hi @andyf65867865 ,

Use the loop in the reverse direction.

 

var doc = app.activeDocument;
processItems(doc.pageItems);

function hasNoFillAndStroke(item) {
    return !item.filled && !item.stroked;
}

function processItems(items) {
    for (var i = items.length - 1; i >= 0; i--) {
        var item = items[i];

        if (hasNoFillAndStroke(item)) {
            alert(item.typename);
            item.remove();
        }
    }
}

 

Best regards