• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Javascript to duplicate pages into other document without creating one large spread

Contributor ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

Hi all,

In need of guidance, again!

This script duplicates all pages from one doc to another:

var source = app.documents[0]; //first active document
var target = app.documents[1]; //second open document
source.pages.everyItem().duplicate(LocationOptions.AFTER, target.pages.item(-1));

However, it puts all the pages into a long spread after the last page:

Screenshot 2022-06-21 at 15.40.10.png

 

If I do it manually from within InDesign using Move Pages – 

With the following options:

All Pages

At End of Document

Delete Pages After Moving - OFF

 

This is what the pages look like afterwards (this is what I'm after):

Screenshot 2022-06-21 at 15.43.24.png

Any ideas how I can overcome this? 

TOPICS
Scripting

Views

95

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jun 21, 2022 Jun 21, 2022

Looks like you don't have Allow Document Spreads to Shuffle selected in the target doc. In that case, just duplicate the spreads: 

var source = app.documents[0]; //first active document
var target = app.documents[1]; //second open document
source.spreads.everyItem().duplicate(LocationOptions.AFTER, target.spreads.item(-1));

 

Likes

Translate

Translate
Contributor , Jun 22, 2022 Jun 22, 2022

I think I've fixed it.

Lines 16 to 19 are now:

var myUSDoc = getDocumentByRegex(/-US/g);

if (myUSDoc != undefined)
    app.activeDocument = myUSDoc;

 ...and more importantly, line 33 has changed to this:

source.spreads.everyItem().duplicate(LocationOptions.BEFORE, target.spreads.item(0));

Note it says BEFORE, not AFTER, and item(0) not item(-1). That fixes the problems I was having. Why? I'm not completely sure. But for now, it works.

 

Likes

Translate

Translate
Community Expert ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

Looks like you don't have Allow Document Spreads to Shuffle selected in the target doc. In that case, just duplicate the spreads: 

var source = app.documents[0]; //first active document
var target = app.documents[1]; //second open document
source.spreads.everyItem().duplicate(LocationOptions.AFTER, target.spreads.item(-1));

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

Hi,

 

Thanks for helping. Spreads sorts the multipage issue. Though my next issue is trying to get the alternate layouts separated as per my previous screenshot. 
The script is putting the pages in the other document in reverse order and only one of them shows as an alternate layout. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

IGNORE THAT. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

I thought I had this working by testing on my MBP but I was using two alternate layouts with the same page size.

Trying to get it working on my iMac with US Letter as one layout and A4 as the other, if I use

source.spreads.everyItem().duplicate(LocationOptions.AFTER, target.spreads.item(-1));

this happens:

Screenshot 2022-06-22 at 19.52.20.png

Page 3 should be page 2 in the A4 layout.

 

If I use:

source.pages.everyItem().duplicate(LocationOptions.AFTER, target.pages.item(-1));

It bunches up as previously discussed.

Screenshot 2022-06-22 at 19.54.16.png

Here's my code with the saving and closing turned off for now. Any ideas how to fix this?

//Allow doc pages to shuffle (avoids multipage spread)
allowShuffleDoc();

function allowShuffleDoc(){
	for(var i = 0; i < app.documents.length; i++){
		myDoc = app.documents[i];
		myDoc.documentPreferences.allowPageShuffle = true;
		}
}

		//if you want to turn on/off spread shuffle, add this:
		//	var spread = app.activeDocument.spreads.everyItem();
		//	spread.allowPageShuffle = true;

//Find doc with "A4" in filename & make it active
var myA4Doc = getDocumentByRegex(/A4/g);

if (myA4Doc != undefined)
    app.activeDocument = myA4Doc;

function getDocumentByRegex(regex) {
    for (var i = 0; i < app.documents.length; i++) {
        var doc = app.documents[i];
        if (regex.test(doc.name))
            return doc;
    }
}

//Duplicate source doc to target doc
var source = app.documents[0]; //active document
var target = app.documents[1]; //second document

source.pages.everyItem().duplicate(LocationOptions.AFTER, target.pages.item(-1));

    // … close source doc
    source.close(SaveOptions.NO);
   
//Save target doc, add -A4 to filename, then close
var target = app.activeDocument; //change target to active doc
var fPath = target.filePath; //get filepath of target doc
var fileName = target.name.replace(/(.*-US)(.*\.indd)/i, "$1-A4$2");
	
//target.save(new File(fPath + "/" + fileName));

    // … and close the document      
	target.close(SaveOptions.NO);

 

On the MBP the pages go in perfectly as per the first screenshot in my first post.

On the iMac the pages bunch up as per the second screenshot.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

LATEST

I think I've fixed it.

Lines 16 to 19 are now:

var myUSDoc = getDocumentByRegex(/-US/g);

if (myUSDoc != undefined)
    app.activeDocument = myUSDoc;

 ...and more importantly, line 33 has changed to this:

source.spreads.everyItem().duplicate(LocationOptions.BEFORE, target.spreads.item(0));

Note it says BEFORE, not AFTER, and item(0) not item(-1). That fixes the problems I was having. Why? I'm not completely sure. But for now, it works.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines