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
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).
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.
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--;
}
}
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).
Copy link to clipboard
Copied
That's awesome, helps tremendously, thanks!
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.
Copy link to clipboard
Copied
It would be great if I could do it all in one click.
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.");
Copy link to clipboard
Copied
Worked like a charm. Thanks so much, you have saved me a ton of time!
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/
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?
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.");
Copy link to clipboard
Copied
If the pages are truly blank except for master page items, would the delete empty pages preference word?
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?
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.
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.
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.
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.
Copy link to clipboard
Copied
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
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?
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
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");
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
Copy link to clipboard
Copied
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.