Remove pages in an array

Participant ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

I have a document that could be 20-100 pages long and I want to delete the pages that I have in an array. When I run this I get crazy results. It deletes pages 8, 10, 12, 14, 19. I don't get that result. Any help would be appriciated.

var myDoc = app.activeDocument;
delArray = [7,8,9,10,14,17];

for (i = 0; i < delArray.length; i++) {
	myDoc.pages.item(delArray[i]).remove(); //Also throws error 'object is invalid'
}

 

 

 

TOPICS
Scripting

Views

107

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

Adobe Community Professional , Jan 16, 2021 Jan 16, 2021
You could try a reverse loop thru the pages, check if the page in the array and then delete , something like this:     var p = app.activeDocument.pages; delArray = [7,8,9,10,14,17]; for(var i = p.length-1; i >= 0; i--){ if (checkItem(delArray, i+1)) { p[i].remove(); } } /** * Checks if an item is in an array * the array to check * the item to look for * true if the item is in the array * */ function checkItem(a, obj) { for (var i = 0; i < a.length; i++) { ...

Likes

translate

Translate

Translate
Adobe Community Professional ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

First sort the array & delete those pages in reverse order...

 

delArray.sort();

for(var i = delArray.length-1; i >= 0; i--){

    if(myDoc.pages.item(delArray[i]).isValid){

        myDoc.pages.item(delArray [i]).remove();

        }

    }

 

Sunil

 

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
Participant ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

So close, it deletes, 8, 9, 10, 14, 18. It gets the center of the array but not the first and last.

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
Adobe Community Professional ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

You could try a reverse loop thru the pages, check if the page in the array and then delete , something like this:

 

 

var p = app.activeDocument.pages;
delArray = [7,8,9,10,14,17];


for(var i =  p.length-1; i >= 0; i--){
    if (checkItem(delArray, i+1)) {
       p[i].remove();
    } 
}


/**
* Checks if an item is in an array
*  the array to check 
*  the item to look for 
*  true if the item is in the array 
* 
*/
function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

 

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
Participant ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

That did it, thanks so much!

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