Skip to main content
MadMich
Inspiring
April 11, 2019
Answered

Delete pages or Extract pages from an Indesign document

  • April 11, 2019
  • 3 replies
  • 6146 views

I need to delete pages from a 40 page Indesign document (i.e. pages 2-13, 15-26, 28-39)

I'm not sure if it can be done but would like to know for sure.
If it's easier to extract the pages required into a new document I'm happy with that option too.

I've used this script but it only does 1 range (2-13) as far as I can see.

Any help would be greatly appreciated.

if (app.documents.length == 0){

  alert("There is no document open to delete pages.");

  exit();

}

var remove_pages = menu();

if (remove_pages.show()) {

number_pages = pages.editContents;

  try {

  remove_override();

  pages_remove();

  } catch (e) {}

}

//functions

//--------------------------------------------------------------------

function menu(){

var dialoog_venster = app.dialogs.add({name:"Delete pages", canCancel:true} );

  with (dialoog_venster){

  with (dialogColumns.add() ){

  with (dialogRows.add() ){

  with (dialogRows.add() ){

  staticTexts.add({staticLabel:"Pagerange you want to delete:       "} );

  }

  }

  with (dialogRows.add() ){

  

  with (dialogColumns.add() ){

  with (dialogRows.add() ){

  pages = textEditboxes.add({editContents:"", minWidth:225} ); }

  }

  }

  }

  }

  return dialoog_venster;

}

function pages_remove() {

  range_test = number_pages.indexOf("-");

  if (range_test != -1) {

  split_range = number_pages.split("-");

  start = split_range[0];

  end = split_range[1];

  number = parseInt(end) - parseInt(start) + 1;

  for (i=0; i<number; i++) {

  page = parseInt(end) - i;

  page = page + "";

  app.activeDocument.pages.item(page).remove();

  }

  } else {

  app.activeDocument.pages.item(number_pages).remove();

  }

}

function remove_override() {

  total_number_pages = app.activeDocument.pages.length;

  for (i=1; i<total_number_pages; i++) {

  page_items = app.activeDocument.pages.item(i).pageItems;

  for (j=page_items.length-1; j>-1; j--) {

  if (page_items.label == "") {

  if (page_items.overridden == true) {

  page_items.removeOverride();

  }

  }

  }

  }

}

This topic has been closed for replies.
Correct answer MadMich

Can you be specific, i did run your code and it seemed to work just fine when i have 2 or more master pages. Please provide more details on whether you get an error or if the operations done by the code are not what you want it to do.

Also the code discussed and verified to be working in this thread seems to be different from the code you have. Did you try the code that's reported in the thread to be working, if not then what extra are you trying to achieve in your code?

-Manan


Hi Manan,

Thanks for your reply.


I've isolated the problem to the master pages and I'm looking at it now. When I override the master page elements it works perfectly well. The test file I sent wasn't a production job and had no master page elements to consider.

For anyone who needs it, the script was used in a mail merge document to isolate individual pages in a 148-page document:
(pages: 1, 13, 25, 37, 50, 62, 74, 86, 99, 111, 123, 135, 148), removing the rest.

Before running the script a few things to check:

Disable Enable in Layout View & Enable in Stroy editor (Indesign/Preferences/Type/Drag and Drop Test Editing).
If multiple master pages, you may need to override as mentioned above.

Run the script and add the unwanted pages into the dialogue box e.g.:

pages 2-12, 14-24, 26-36, 38-49, 51-61, 63-73, 75-85, 87-98, 100-110, 112-122, 124-134, 136-147


This will then process leaving only 13 pages in the document:
pages 1, 13, 25, 37, 50, 62, 74, 86, 99, 111, 123, 135, 148

Thanks for all your help with this…

————————————————————————————

if (app.documents.length == 0){

  alert("There is no document open to delete pages.");

  exit();

}

var remove_pages = menu();

if (remove_pages.show()) {

number_pages = pages.editContents;

  try {

  remove_override();

  pages_remove();

  } catch (e) {}

}

//functions

//--------------------------------------------------------------------

function menu(){

var dialoog_venster = app.dialogs.add({name:"Delete pages", canCancel:true} );

  with (dialoog_venster){

  with (dialogColumns.add() ){

  with (dialogRows.add() ){

  with (dialogRows.add() ){

  staticTexts.add({staticLabel:"Pagerange you want to delete:       "} );

  }

  }

  with (dialogRows.add() ){

 

  with (dialogColumns.add() ){

  with (dialogRows.add() ){

  pages = textEditboxes.add({editContents:"", minWidth:225} ); }

  }

  }

  }

  }

  return dialoog_venster;

}

function pages_remove() 

    range_test = number_pages.replace(/(\d+)-(\d+)/g, function(a,b,c) { a = b; b = Number(b); c = Number(c); for (b++; b<=c; b++) a+=','+b; return a}).split(","); 

    var pg = app.activeDocument.pages.everyItem().getElements() 

    for (i=0; i<range_test.length; i++) 

    { 

          pg[parseInt(range_test) - 1].remove(); 

    } 

}

function remove_override() {

  total_number_pages = app.activeDocument.pages.length;

  for (i=1; i<total_number_pages; i++) {

  page_items = app.activeDocument.pages.item(i).pageItems;

  for (j=page_items.length-1; j>-1; j--) {

  if (page_items.label == "") {

  if (page_items.overridden == true) {

  page_items.removeOverride();

  }

  }

  }

  }}

————————————————————————————

3 replies

Brainiac
May 18, 2020

Original code (from my email subscription) 

if (app.documents.length == 0){
  alert("There is no document open to delete pages.");
  exit();
}
 
var remove_pages = menu();
 
if (remove_pages.show()) {
number_pages = pages.editContents;
  try {
  remove_override();
  pages_remove();
  } catch (e) {}
}
 
//functions
//--------------------------------------------------------------------
function menu(){
var dialoog_venster = app.dialogs.add({name:"Delete pages", canCancel:true} );
  with (dialoog_venster){
  with (dialogColumns.add() ){
  with (dialogRows.add() ){
  with (dialogRows.add() ){
  staticTexts.add({staticLabel:"Pagerange you want to delete:       "} );
  }
  }
 
  with (dialogRows.add() ){
 
  with (dialogColumns.add() ){
  with (dialogRows.add() ){
  pages = textEditboxes.add({editContents:"", minWidth:225} ); }
  }
  }
 
  }
 
  }
  return dialoog_venster;
}
 
function pages_remove() 
{ 
    range_test = number_pages.replace(/(\d+)-(\d+)/g, function(a,b,c) { a = b; b = Number(b); c = Number(c); for (b++; b<=c; b++) a+=','+b; return a}).split(","); 
    var pg = app.activeDocument.pages.everyItem().getElements() 
    for (i=0; i<range_test.length; i++) 
    { 
          pg[parseInt(range_test[i]) - 1].remove(); 
    } 
}
 
function remove_override() {
  total_number_pages = app.activeDocument.pages.length;
  for (i=1; i<total_number_pages; i++) {
  page_items = app.activeDocument.pages.item(i).pageItems;
  for (j=page_items.length-1; j>-1; j--) {
  if (page_items[j].label == "") {
  if (page_items[j].overridden == true) {
  page_items[j].removeOverride();
  }
  }
  }
 
  }}

 

Matt Cass
Participating Frequently
May 18, 2020

Thank you both

 

The above original code has resolved the issue and the script now works.

Adobe Expert
May 18, 2020

Hi Matt,

did you recently copy the code from this thread to the ESTK or a different editor?

If so it could be that all this posted code here in the thread is "corrupt".

Means: The code has changed in subtle or not so subtle ways when moving this thread to the new Adobe InDesign Forum last year.

 

Let's hope that the the original poster, MadMich, will stop by and will correct the now presented code.

 

That mess happened to a lot with old threads, pre September 2019, presenting ExtendScript code:

InDesign, Illustrator, PhotoShop and other Adobe forums.

 

Regards,
Uwe Laubender

( ACP )

Jongware
Adobe Expert
April 11, 2019

See set conditional text  – scroll down to my answer for a short snippet that uses a custom replace function to change any from-to range in a comma separated list. Works with multiple ranges as well; it might be all you need.

MadMich
MadMichAuthor
Inspiring
April 12, 2019

Thank you for your reply
I've tried inserting your snippet into my code but I'm not having much luck!

Where about should I be inserting your code?

Thanks again

MadMich
MadMichAuthorCorrect answer
Inspiring
June 5, 2019

Can you be specific, i did run your code and it seemed to work just fine when i have 2 or more master pages. Please provide more details on whether you get an error or if the operations done by the code are not what you want it to do.

Also the code discussed and verified to be working in this thread seems to be different from the code you have. Did you try the code that's reported in the thread to be working, if not then what extra are you trying to achieve in your code?

-Manan


Hi Manan,

Thanks for your reply.


I've isolated the problem to the master pages and I'm looking at it now. When I override the master page elements it works perfectly well. The test file I sent wasn't a production job and had no master page elements to consider.

For anyone who needs it, the script was used in a mail merge document to isolate individual pages in a 148-page document:
(pages: 1, 13, 25, 37, 50, 62, 74, 86, 99, 111, 123, 135, 148), removing the rest.

Before running the script a few things to check:

Disable Enable in Layout View & Enable in Stroy editor (Indesign/Preferences/Type/Drag and Drop Test Editing).
If multiple master pages, you may need to override as mentioned above.

Run the script and add the unwanted pages into the dialogue box e.g.:

pages 2-12, 14-24, 26-36, 38-49, 51-61, 63-73, 75-85, 87-98, 100-110, 112-122, 124-134, 136-147


This will then process leaving only 13 pages in the document:
pages 1, 13, 25, 37, 50, 62, 74, 86, 99, 111, 123, 135, 148

Thanks for all your help with this…

————————————————————————————

if (app.documents.length == 0){

  alert("There is no document open to delete pages.");

  exit();

}

var remove_pages = menu();

if (remove_pages.show()) {

number_pages = pages.editContents;

  try {

  remove_override();

  pages_remove();

  } catch (e) {}

}

//functions

//--------------------------------------------------------------------

function menu(){

var dialoog_venster = app.dialogs.add({name:"Delete pages", canCancel:true} );

  with (dialoog_venster){

  with (dialogColumns.add() ){

  with (dialogRows.add() ){

  with (dialogRows.add() ){

  staticTexts.add({staticLabel:"Pagerange you want to delete:       "} );

  }

  }

  with (dialogRows.add() ){

 

  with (dialogColumns.add() ){

  with (dialogRows.add() ){

  pages = textEditboxes.add({editContents:"", minWidth:225} ); }

  }

  }

  }

  }

  return dialoog_venster;

}

function pages_remove() 

    range_test = number_pages.replace(/(\d+)-(\d+)/g, function(a,b,c) { a = b; b = Number(b); c = Number(c); for (b++; b<=c; b++) a+=','+b; return a}).split(","); 

    var pg = app.activeDocument.pages.everyItem().getElements() 

    for (i=0; i<range_test.length; i++) 

    { 

          pg[parseInt(range_test) - 1].remove(); 

    } 

}

function remove_override() {

  total_number_pages = app.activeDocument.pages.length;

  for (i=1; i<total_number_pages; i++) {

  page_items = app.activeDocument.pages.item(i).pageItems;

  for (j=page_items.length-1; j>-1; j--) {

  if (page_items.label == "") {

  if (page_items.overridden == true) {

  page_items.removeOverride();

  }

  }

  }

  }}

————————————————————————————