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

Script for removing blank pages

Explorer ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

7.7K

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

Enthusiast , Dec 18, 2019 Dec 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).

Votes

Translate

Translate
Community Expert ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

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.

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
Explorer ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

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--;

     }

}

 

 

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
Enthusiast ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

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

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
Explorer ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

That's awesome, helps tremendously, thanks!

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
Enthusiast ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

Great, glad to be of help. However, this doesn't address your other note about pages have a single text frame with... ? Not sure what it contains. Page number? Or is it just an empty text frame? So also removes pages that have a single empty text frame? If I knew more I could add code to detect this excess frame you don't want. Or perhaps what I posted is enough to get you there, and you've solved the excess frame already.

William Campbell

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
Explorer ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

It would be great if I could do it all in one click.

 
See screen shots below.
 
All of the pages will have a sequence number in the bottom left. Note, these are not numbered pages, they are variable.
 
I want to keep the pages that have copy in them, like the first screenshot, and delete the pages that only have the sequence number, second screenshot.
 
Page size is 5.25" wide x 4" tall.
 
Sequence number is in the lower left within .25 inches from the corner.
 
Screen Shot 2019-12-18 at 2.25.49 PM.pngScreen Shot 2019-12-18 at 2.26.00 PM.png

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
Enthusiast ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

OK, I will make an assumption -- pages you want to keep have the sequence number frame AND a frame of copy, so safe to assume a "wanted" page is at least 2 text frames, regardless of what they contain. The pages you don't want are either entirely blank or have only 1 text frame (sequence number). Simpliest approach works on this assumption of number of text frames. If I'm on the right track, revised code below should work. It adds a test for the count of textFrames on the page is less than 2, means delete the page. Let me know if that does the trick. Still ignores master pages and pasteboard. And if you have pages you do want, and do not have the sequence number frame (so only have a one frame, which is copy you want to keep), this isn't going to work right. Just a warning, so be sure to have a back up of the file before trying it.

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.textFrames.length < 2) {
        page.remove();
    }
}
alert("Done.");

 

William Campbell

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
Explorer ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

Worked like a charm. Thanks so much, you have saved me a ton of time!

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
Enthusiast ,
Dec 18, 2019 Dec 18, 2019

Copy link to clipboard

Copied

Awesome. Glad to help. Have a look at my other free scripts that save a ton of time:

https://www.marspremedia.com/software/indesign/

 

William Campbell

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 Beginner ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

Is it possible to create a version of this that judges based on whether there is text on a page? Deleting pages only with no text?

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
Enthusiast ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

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.remove();
    }
}
alert("Done.");
William Campbell

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 ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

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)

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 Beginner ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

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?

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
Enthusiast ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

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

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 Beginner ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

Thanks, William. This is really helpful! I will try the scripts you've suggested here. I'll see if I can't call on some others more versed in Indesign for help thinking through the other thing you suggested.

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
Enthusiast ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

The trouble is (I think anyway, from the impression I'm getting here) is that your data merge is a considerable amount of text that sometimes overflows the space alloted for it. That's unusual and is beyond the capability of data merge, which is more for various small amounts of text each positioned once on each page. That's why I say it's possible but would be better handled by a script specific to the task. But I'd have to know the task, or really, what goes in and what is the desired result. Then maybe I'd get a bright idea for script code that can do it.

 

William Campbell

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 Beginner ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

Your script worked like a charm. Thank you again! And, to clarify my last comment, I meant that I am going to ask someone else on my team who knows more about InDesign than me to help me try your other suggestion.

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
Explorer ,
Oct 07, 2022 Oct 07, 2022

Copy link to clipboard

Copied

MartinPH_0-1665133862588.png

Hi William, 
How about in this case (as per image); 1 text frame contains hidden contents (via condition). Making the whole page look like blank but InDesign treats it as not blank since there are contents hidden in it. I dont wish to delete them, just want to have an alert message the there is a blank page in the file.

Martin

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
Enthusiast ,
Oct 07, 2022 Oct 07, 2022

Copy link to clipboard

Copied

Select the text frame and alert that "page # is blank".

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.textFrames[0].select();
        alert("Page " + page.name + " is blank");
    }
}
alert("Done");

Does that what you were thinking?

 

William Campbell

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
Explorer ,
Oct 09, 2022 Oct 09, 2022

Copy link to clipboard

Copied

Hi William,
Thanks for your reply.
My query is something about this line:
if (page.textFrames.length == 0 || page.textFrames[0].contents.length == 0)

It targets 2 types of blank page:
1. Totally Blank Page
2. Page With Textframe without any content in it.

My query is to find the 3rd type, Page with Textframe with hidden contents in it (hidden via conditional text). 
The scenario is this: the previous page is totally full that it pushes InDesign to create additional page via smart reflow. The excess content is just a condition marker but it needs to have it's own text frame to avoid overset. 

Now, I wish to be alerted when this occurs so I can make adjustments for the layout. I dont know if this is possible but I am trying my chances :).

If there's an option to locate textframes with only hidden contents in it. 

Martin

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
Enthusiast ,
Oct 10, 2022 Oct 10, 2022

Copy link to clipboard

Copied

Add test for hidden texts and length of frame contents is 1 (the condition marker, nothing more). I also made the loop go forward, normal, typical, because now not deleting items so no need to go backward.

var doc = app.activeDocument;
var i;
var page;
for (i = 0; i < doc.pages.length; i++) {
    page = doc.pages[i];
    if (page.textFrames.length == 0 || page.textFrames[0].contents.length == 0) {
        page.textFrames[0].select();
        alert("Page " + page.name + " is blank");
    } else if (page.textFrames[0].hiddenTexts.length && page.textFrames[0].contents.length == 1) {
        page.textFrames[0].select();
        alert("Page " + page.name + " is blank due to hidden text");
    }
}
alert("Done");

 

William Campbell

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
Explorer ,
Oct 10, 2022 Oct 10, 2022

Copy link to clipboard

Copied

Thanks for this, William. Will try this as soon as a get my hands on a computer. Will let you know.

Martin

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
New Here ,
May 24, 2023 May 24, 2023

Copy link to clipboard

Copied

LATEST

Hi William,

I have a doubt. If I have a continued table with no identity like no contents in textframe, no groups, no rectangle frames but has only continued tables spanned to multiple pages. I should ignore the blank page deletion if textframe contains only spanned tables.
I tried but no luck.

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