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:
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):
Any ideas how I can overcome this?
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));
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.
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));
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.
Copy link to clipboard
Copied
IGNORE THAT.
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:
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.
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.
Copy link to clipboard
Copied
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.