• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Remove pages in an array

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

321

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

Community Expert , 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++) {
        
...

Votes

Translate

Translate
Advocate ,
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

 

Votes

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

Votes

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
Community Expert ,
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;
}

 

Votes

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

Copy link to clipboard

Copied

LATEST

That did it, thanks so much!

Votes

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