Search code examples
javascriptscriptingadobe-indesign

InDesign script to add a page to every given page


I have an InDesign with ~400 pages and need a rather simple script that adds a page to every existing page (so: p1+1; p2+1; p3+1 ...). Also, the added page should contain two text frames. I'm using InDesign CS6 and JS.

I have some lines, but not the whole thing:

// the start
var myDocument = app.activeDocument;

// the loop
for(var i = 0; i < myDocument.pages.length; i++) {}

// text frames
var rect = myDocument.pages.item(0).rectangles.add();
var rect2 = myDocument.pages.item(1).rectangles.add();

var gb = [125.891, 25, 230.458, 88];
var gb2 = [125.891, 94, 230.458, 157];

rect.geometricBounds = gb;
rect2.geometricBounds = gb2;

Solution

  • There are two similar approaches I can think of. Loop the number of the pages times and add pages at the correct location or add all the pages at the end and move them to correct location with move(). Try this:

    var myDocument = app.activeDocument;
    
    var addPageLocation = 0;  
    
    var docLength = myDocument.pages.length;  
    // every time yo add a page pages.length will increase
    
    // the loop
    for(var i = 0; i < docLength; i++) {
    
        myDocument.pages.add(LocationOptions.AFTER, myDocument.pages[addPageLocation] );
    
        //you will need to add text frame as rectangles are used for graphics or pdfs
    
        var tf = myDocument.pages[addPageLocation+1].textFrames.add();
        var tf2 = myDocument.pages[addPageLocation+1].textFrames.add();
    
        var gb = [125.891, 25, 230.458, 88];
        var gb2 = [125.891, 94, 230.458, 157];
    
        tf.geometricBounds = gb;
        tf2.geometricBounds = gb2;
    
        //get the new page location including the page which was added
        addPageLocation = addPageLocation+2;
    
    }