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

3.5K

Likes

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

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

Likes

Translate

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

Likes

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

     }

}

 

 

Likes

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

Likes

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!

Likes

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

Likes

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

Likes

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

Likes

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!

Likes

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

Likes

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?

Likes

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

Likes

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, Adobe Certified Instructor, and Adobe Certified Expert (since 1995)

Likes

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?

Likes

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

Likes

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.

Likes

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

Likes

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

LATEST

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.

Likes

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

Save time with Adobe Express quick actions