Skip to main content
Inspiring
May 24, 2023
Answered

Validate and fix a Book's total page count via script

  • May 24, 2023
  • 4 replies
  • 779 views

I am working on a process to automate the production of a multiple directories. As part of that process I have built a script that assembles my books and exports them to PDF. However, I need to make sure that the total page count of each book is a multiple of four(4) for printing purposes.

I know how to get the page count of an individual document, but I have not found a good way to get the total page count of a book. All of the documentation on Preflight scripting appears to be specific to a single document.

Ideally, if the page count is not a multiple of 4, the script would add extra pages to the last document in the book as well..

Any suggestions?

This topic has been closed for replies.
Correct answer Peter Kahrel

It's not as simple as that, Mark. documentPageRange returns a string, which can be a single page number or a range, as in 10-20. But you can have roman numbers as well (i-xi), or a mix, so keep thing funny (iii-12). So you have to open each document and get the page count in each document, as Robert suggested or not open any document and calculate the page extent of each document from its page range.

 

Robert's method is easy to script and is relatively quick (especially if you don't show the documents). The second method is not so easy to implement but is very quick. It's an old script, see attached. I used the txt file type because jsx isn't allowed. Change it back to jsx before using it.

 

P.

4 replies

m1b
Community Expert
Community Expert
May 25, 2023

I also wondered if Book.repaginationOption might be a factor. For example, I don't know what happens if a book document [1] ends on, say, an even page while the book's repagination option is RepaginateOption.NEXT_EVEN_PAGE. Does it add one blank page to book document [1] when a second document is added to the book? If not, then you would need to add that page to your total (and maybe even explicitly add it to document [1]).

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
May 25, 2023

It's not as simple as that, Mark. documentPageRange returns a string, which can be a single page number or a range, as in 10-20. But you can have roman numbers as well (i-xi), or a mix, so keep thing funny (iii-12). So you have to open each document and get the page count in each document, as Robert suggested or not open any document and calculate the page extent of each document from its page range.

 

Robert's method is easy to script and is relatively quick (especially if you don't show the documents). The second method is not so easy to implement but is very quick. It's an old script, see attached. I used the txt file type because jsx isn't allowed. Change it back to jsx before using it.

 

P.

Inspiring
May 25, 2023

Thanks All! Very much appreciated! I will give these solutions a try and let you know how it goes!

m1b
Community Expert
Community Expert
May 25, 2023

Hi @Ben Ross - KP, here's something to get you started for now. You can look at the API for Book and BookContent, too.

- Mark

 

var book = app.books[0];

if (!book)
    return;

for (var i = 0; i < book.bookContents.length; i++) {
    var bc = book.bookContents[i];
    $.writeln('bc.documentPageRange = ' + bc.documentPageRange);
}
Robert at ID-Tasker
Legend
May 25, 2023

You need to go through the collection of the documents of the book and read no of pages and check the total.