Skip to main content
Inspiring
March 16, 2023
Answered

To place a few pages of an indd document to another document

  • March 16, 2023
  • 1 reply
  • 705 views

var targetDoc = app.activeDocument;

var sourceFile = File.openDialog("select indd file");

targetDoc.pages[0].place(sourceFile);

// this is working fine as it place first page on indd file

 

var sourceDoc = app.open(sourceFile);

for (i =0; i < sourceDoc.pages.length; i++)

{

var page = sourceDoc.pages[i]'

targetDoc.pages.add.place(page);  /// here I receive error => Invalid value for parameter. expected File but received page. Any suggestions are welcome.

 

}

 

This topic has been closed for replies.
Correct answer m1b

Hi @virender_CTS, you can do it like this:

function main() {

    var targetDoc = app.activeDocument;
    var sourceFile = File.openDialog("select indd file");
    if (sourceFile == null) return;
    var sourceDoc = app.open(sourceFile);

    app.importedPageAttributes.importedPageCrop = ImportedPageCropOptions.CROP_CONTENT;
    for (var i = 0; i < sourceDoc.pages.length; i++) {
        app.importedPageAttributes.pageNumber = i + 1;
        var importedPage = targetDoc.pages.add().place(sourceFile, [0, 0])[0];
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Import Indesign Document');

 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
March 16, 2023

Hi @virender_CTS, you can do it like this:

function main() {

    var targetDoc = app.activeDocument;
    var sourceFile = File.openDialog("select indd file");
    if (sourceFile == null) return;
    var sourceDoc = app.open(sourceFile);

    app.importedPageAttributes.importedPageCrop = ImportedPageCropOptions.CROP_CONTENT;
    for (var i = 0; i < sourceDoc.pages.length; i++) {
        app.importedPageAttributes.pageNumber = i + 1;
        var importedPage = targetDoc.pages.add().place(sourceFile, [0, 0])[0];
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Import Indesign Document');

 

Inspiring
March 16, 2023

Hi m1b, it worked like a magic, thank you very much.

Just to tell you, i have changed this code from

targetDoc.pages.add().place(sourceFile, [0, 0])[0]; to

targetDoc.pages[i].place(sourceFile, [0, 0])[0]; 

 

Additionally, if you can spend fewmore mins then please explain the meaning of code in red below.

targetDoc.pages.add().place(sourceFile, [0, 0])[0];

m1b
Community Expert
Community Expert
March 16, 2023

Hi @virender_CTS, the code in red:

1. targetDoc.pages.add() - you already know this part—it adds a new page and returns a Page specifier object.

2..place(sourceFile, [0, 0]) - the place method of Page—see documentation here. Takes a File, and a placePoint (I used coordinates). It has other parameters after the second which I didn't use, but you may like to set them.

3. [0] - the index of the first item of the array returned by Page.place method (in this case a single ImportedPage). Therefore if you are not capturing a reference to the placed object, then you have removed "var importedPage =" you can also remove the trailing "[0]". It isn't needed in your code.

- Mark