Skip to main content
Participating Frequently
December 18, 2019
Answered

Script for removing blank pages

  • December 18, 2019
  • 2 replies
  • 9267 views

Does anyone have a script that will remove blank pages from InDesign?

 

I have a data merge that produces hundreds of blank pages that need to be removed. It's complicated so I won't go into why..

 

An extra bonus would be if the script would remove a page with an empty text box. Each page has a sequence number so the entire page is not actually blank. However, I could get by without the sequence number, adding it later.

 

CC - 2019

This topic has been closed for replies.
Correct answer willcampbell7
var doc = app.activeDocument;
var i;
var page;
for (i = doc.pages.length - 1; i > 0; i--) {
    page = doc.pages[i];
    if (page.allPageItems.length == 0) {
        page.remove();
    }
}
alert("Done.");

Caution: This simple example will not consider items on master page or off page on pasteboard. If either exists, this code still considers the page "blank" when the property allPageItems array is emtpy, and will delete the page, but never page 1 (documents must have minimum 1 page).

2 replies

Dave Creamer of IDEAS
Community Expert
Community Expert
August 19, 2022

If the pages are truly blank except for master page items, would the delete empty pages preference word?

David Creamer: Community Expert (ACI and ACE 1995-2023)
Participant
August 19, 2022

Maybe there's a better solution to my larger issue, which is that I would like to place data merge fields into the master primary text frame so that it will auto reflow and add more pages as necessary when the fields create overset text. Whenever I try to add data merge fields to the Master Primary Text Frame it takes away the Primary text frame status. I decided to try this without using the master page. I tried just adding a bunch of empty pages with threaded text boxes in my regular spreads to account for the overset text, but then for those that are shorter, I am left with extra blank pages.

 

Is there any way to create a document with data merge where the pages automatically overflow text and create more pages as necessary?

willcampbell7
Legend
August 19, 2022

Answer to your first question... if no text frame, delete. OR, if the first text frame (we assume the only frame) exists but has no contents, delete. So this revised version of the script will remove the extra pages that have a single frame but the text never flowed into.

var doc = app.activeDocument;
var i;
var page;
for (i = doc.pages.length - 1; i > 0; i--) {
    page = doc.pages[i];
    if (page.textFrames.length == 0 || page.textFrames[0].contents.length == 0) {
        page.remove();
    }
}
alert("Done.");

Now, your second question, doing some kind of data merge that autoflows text. Yes something like that should be possible. But I wouldn't use data merge. I'd do my own data import from a CSV file. There would be much to figure out about how it would work exactly depending on what it needs to do. And something like that isn't a few simple lines.

 

William Campbell
Jongware
Community Expert
Community Expert
December 18, 2019

I am confused. No, I don't have a script that removes blank pages but it's really, really trivial to write one.

However, you seem to have a lot of items on your blank pages so it becomes non-trivial after all. Care to expand on your exact requirements? Then maybe somebody may be able to help you out.

Participating Frequently
December 18, 2019

I guess at minimum I would like a script to remove pages in my document. Pages that do not contain object or text.

 

If I knew anything about scripting I'm sure it would be very simple. I have some knowledge of if/then statements but not enough to do me any good here.

 

Someone on this forum posted a script they created to remove blank pages but it keeps giving me the error "Object does not support the property or method "textFrames""

 

Script below:

 

doc=app.activeDocument;

pageObj=doc.pages.item(0);

txtObj=pageObj.textFrames.item(0);

var temp=doc.pages.length;

for(var i=0;i<temp;i++)

{

     if(doc.pages.textFrames.item(0)!=null)

     {

          if(doc.pages.textFrames[0].contents=="")

          {

               doc.pages.remove();

               i=i-1;

               temp--;

          }

          else

          {

          }

     }

     else

     {

          doc.pages.remove();

          i=i-1;

          temp--;

     }

}

 

 

willcampbell7
willcampbell7Correct answer
Legend
December 18, 2019
var doc = app.activeDocument;
var i;
var page;
for (i = doc.pages.length - 1; i > 0; i--) {
    page = doc.pages[i];
    if (page.allPageItems.length == 0) {
        page.remove();
    }
}
alert("Done.");

Caution: This simple example will not consider items on master page or off page on pasteboard. If either exists, this code still considers the page "blank" when the property allPageItems array is emtpy, and will delete the page, but never page 1 (documents must have minimum 1 page).

William Campbell