Script to duplicate page/spread after current
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to duplicate page/spread after current
Tagged: duplicate page javascript
- This topic has 34 replies, 1 voice, and was last updated 2 years, 7 months ago by J D.
-
AuthorPosts
-
-
January 12, 2013 at 10:01 am #63925Mark ChildersMember
In using ID to create presentations, I frequently need to duplicate the current page or spread and have the copy appear right after the current page/spread (rather than at the end of the document). I currently Alt drag on the page in the pages panel, but that is a bit ticky–especially when having to do it over and over–because you have to drag to just the magic place where the vertical bar appears between the two pages.
A long lead up to this: Does anyone have a script to automate this function?
Thanks.
Mark
-
January 14, 2013 at 3:12 am #63939Theunis De JongMember
It's suprisingly easy. The scripting interface knows what 'layout window' you are looking at (in case you have several windows and/or documents opened), and it knows what page or spread is selected inside that window. Both a Spread and a Page have a “duplicate” method, which does exactly what it says — always a nice surprise –, and you also can indicate where the duplicate should be placed.
This one-liner will duplicate the current spread:
app.layoutWindows[0].activeSpread.duplicate (LocationOptions.AFTER, app.layoutWindows[0].activeSpread);and this one the current page:
app.layoutWindows[0].activePage.duplicate (LocationOptions.AFTER, app.layoutWindows[0].activePage);Note that in the latter case it's slightly ambiguous what the 'current page' is when you are viewing a double spread. When both pages are selected in the Pages panel, the script uses the leftmost page. To be sure what page gets duplicated, use Shift+PgUp and Shift+PgDn to zoom in on the specific page you want to duplicate.
Best of all: if you need this often, well, you can bind shortcut keys to scripts.
-
November 15, 2013 at 9:06 am #66202Jennifer FawleyMember
Hello,
How do I use this “one-liner”, copy and paste it somewhere? -
November 18, 2013 at 3:06 am #66215Theunis De JongMember
Jennifer, see David’s https://creativepro.com/how-to-install-a-script-in-indesign-that-you-found-in-a-forum-or-blog-post.php —
“This has bugged novice InDesign users for years: You find a script on a forum thread or a blog post, but it’s just text… that is the scripter has pasted the text of the script in, so you can actually see the code. For experienced users (and scripters), that’s no big deal. But if you want to use that script, but you don’t know what to do with the code, read on.”
-
October 26, 2016 at 1:40 pm #89332Aaron RussellMember
Hi Theunis,
Thank you so much for that script! It works wonders. Curious, can you tell me how to add something to the script to make the duplicated page active instead of staying on the original page?
Thanks!
-
-
January 16, 2013 at 2:24 am #63951Mark ChildersMember
Works like a charm . . . keyboard shortcut and all. Thanks!
-
May 14, 2014 at 7:52 am #68429gprovanMember
I know this was a while ago. However, although this is a great script, does anyone know how to select all pages in a document and create a duplicate directly after each page? This only seems to work with individual pages and I regularly have documents with over 100 pages which I’d like to have 2 copies of each page beside each other.
Thanks!
-
-
August 20, 2014 at 4:51 pm #70198gamouningMember
I too have need of a script that selects all pages and duplicates them after each page; instead of at the end of the document. Anyone create this script yet?
-
March 1, 2019 at 9:16 am #114690Lasse BrendenMember
Someone might find the following script useful.
It duplicates each page in place instead of appending the duplicates to the end of the document (e.g. 1-2-3 becomes 1-1-2-2-3-3).
—
// Define variables
var file = app.activeDocument;
var pages = file.pages;
var numberOfPages = pages.length;// Duplicate each page
for (i = 0; i < numberOfPages; i++) {
file.pages[i].duplicate();
}// Intertwine pages
for (var i = 0; i < (numberOfPages – 1); i++) {
var currentPage = (numberOfPages + i);
var insertPoint = (i + i);file.pages[currentPage].move(LocationOptions.AFTER, file.pages[insertPoint]);
}—
-
-
August 27, 2014 at 1:29 pm #70280Matt MayerchakParticipant
Thanks for the freebie!
When I make invoices, I often find what I want is to copy a previous invoice and make a new page in front of the current first page of the document. So, I just tried changing the word “AFTER” in Jongware’s script to BEFORE as follows:
app.layoutWindows[0].activePage.duplicate (LocationOptions.BEFORE, app.layoutWindows[0].activePage);
That works – but now, it puts the page in front of the current one. So, If I’m on page 1, it dupes page one in front of itself. That’s cool, but not much better than duping right after itself.
If I am on page 20, and want to dupe that page to the start of the document (a new page 1), what word would I use instead of BEFORE?
-
September 1, 2014 at 6:16 am #70346Lars OscarssonMember
Hi, i think you could try:
app.layoutWindows[0].activePage.duplicate (LocationOptions.AT_BEGINNING, app.activeDocument.pages[0]); -
September 16, 2014 at 7:15 pm #70632Matt MayerchakParticipant
Laros – that worked great – thanks!
Note – I tried changing just the text “BEFORE,” to “AT_BEGINNING,” but it didn’t work. I had to replace the whole line of text, to include the “app.activeDocuments.pages[0]);” part instead of what was there.
But now, using the one-line script you posted above, it duplicates the current page at the front of the document. Very nice!
Haven’t tried it on a spread – but will let you know if it works differently on facing pages. Mostly need it for single-page docs.
i will use this frequently – thank you!
Matt
-
October 22, 2014 at 1:35 pm #71190Michael ZaichenkoMember
The only problem I find with this script is that when you duplicate multiple pages at once with itemByRange(), they all come in as one spread, i.e. if I duplicated 10 pages, they would be part of a single spread in the destination document. On the other hand if I duplicate spreads by range, they lose text threading from one spread to another, therefore defeating the whole purpose of scripting it. Any ideas on this? I’m trying to combine several threaded documents into one with these but run into aforementioned issues:
var initDoc = app.open(File (Doc1.indd));
for (b = 1; b < Docs.length; b++){
var pg = initDoc.spreads.lastItem();
var nxtDoc = app.open(File (Docs[b]), false);
nxtDoc.spreads.itemByRange(0, nxtDoc.spreads.length-1).duplicate(LocationOptions.AT_END, pg);
nxtDoc.close(SaveOptions.NO);
}
ORvar initDoc = app.open(File (Doc1.indd));
for (b = 1; b < Docs.length; b++){
var pg = initDoc.pages.lastItem();
var nxtDoc = app.open(File (Docs[b]), false);
nxtDoc.pages.itemByRange(0, nxtDoc.spreads.length-1).duplicate(LocationOptions.AT_END, pg);
nxtDoc.close(SaveOptions.NO);
}Michael
-
March 30, 2016 at 3:52 am #83460Muhammad WaheedMember
Dear Jongware,
can i use your code to duplicate the whole pages or the main document with reversed order
Thanks in advance
-
April 26, 2016 at 4:48 am #84293LostthehyphenMember
Hi All,
Super interesting thread. I’ve been looking for a way to 2-up my InDesign documents to print for a while now. ‘2-up’ in the way that it prints 1-1, 2-2, 3-3, etc. Rather than 1-2, 3-4, 5-6, which is what it does now. I figured if I can make a version of my documents that have all the spreads/pages duplicated, I can Indesign 2-up my way to the desired result. All the info seems to be here, but unfortunately I’m a Mac user; would anyone know the scripts for a Mac?
I would be ever so grateful. Thanks a lot in advance.
-
April 26, 2016 at 5:57 am #84294Matt MayerchakParticipant
The scripts discussed are javascripts, which are cross-platform, so they work on both mac and windows. I work on a Mac, so I know it works.
Applescripts only work on Macs, which is why the vast majority of scripts discussed and shared online for InDesign are javascripts.
I hope that helps!
-
April 26, 2016 at 6:05 pm #84344Michael ZaichenkoMember
Depending on the version of InDesign you are running, you could use different Layouts in the same document. I think it should be possible in CC14 and CC15 and maybe even in CS6. In such a case you would have the same text “linked” to two different ways of displaying it. Standard imposing though is better done on PDF files in Acrobat but that depends on your production flow. If you are interested in a script that will make a copy of your file with pages imposed, it probably can be done too – is that what you are trying to accomplish?
Michael
-
April 29, 2016 at 3:34 am #84420LostthehyphenMember
Thanks, Matt – I will try it! The word “Windows” in the script threw me off ;)
And yes, Michael, that is what I’m trying to accomplish. It’s actually exactly what the original forum post described, I just thought the scripts were written for Windows that’s all!
I’ll give it a go, thanks for your replies!All the best,
-
November 3, 2016 at 9:39 am #89562Aaron RussellMember
Hi all!
Theunis’s script works perfectly! Curious, if any of you can tell me how to add any code to the script so that the duplicated page becomes active instead of staying on the original page?
Thanks!
-
November 3, 2016 at 12:17 pm #89579Michael ZaichenkoMember
You could use:
app.activeWindow.activePage = app.activeDocument.pages [yourPageIndex];
or
app.activeWindow.activePage = app.activeDocument.pages.itemByName (“Your page number”);
Michael
-
November 3, 2016 at 12:33 pm #89580Aaron RussellMember
Hi Michael,
Thanks so much for the response. So can you tell me where to insert it in this script? I’m sorry, just really a novice when it comes to this stuff!
app.layoutWindows[0].activePage.duplicate (LocationOptions.AFTER, app.layoutWindows[0].activePage);
-
-
November 3, 2016 at 1:12 pm #89583Michael ZaichenkoMember
Something like that:
app.activeWindow.activePage = app.layoutWindows[0].activePage.duplicate (LocationOptions.AFTER, app.layoutWindows[0].activePage);
or
app.activeWindow.activePage = app.activeWindow.activePage.duplicate (LocationOptions.AFTER, app.activeWindow.activePage);
-
November 3, 2016 at 1:30 pm #89584Aaron RussellMember
Worked perfectly! Thanks, Michael!
-
-
February 21, 2019 at 7:26 am #114511Tamás NagyParticipant
Hi all,
Michael asked some posts and 5 years ago how to duplicate multiple spreads in a way that preserves text threads from one spread to another. I have the same problem now, but I couldn’t find the answer here. Is here anyone who knows the solution? :)
I tried to copy the spreads one by one by a for cycle, no success. Then I tried to copy them in 1 step this way:
var myS=thisDoc.spreads.everyItem();
myS.duplicate(LocationOptions.AT_END, otherDoc);but no change, it didn’t work either: all interspread threads were broken.
Someone please help me!
Tamás
-
March 2, 2019 at 3:10 am #114705Tamás NagyParticipant
Hi again,
I’ve got an e-mail that said Lasse answered my question. But as I see there are no new answers here. Lasse’s post that was mentioned in the mail doesn’t solve my problem. It is a a great script but it still breaks text threads and ONLY this was my problem, not simply duplicating those pages.
I’m still seeking the solution if it exists at all. When I drag the spreads from one document into another by hand, all threads remain in place. The same action done from script works differently: it breaks all interspread threads.
Tamás
-
September 26, 2019 at 3:33 am #14324059Jelle Wouter VerschurenMember
To get this topic alive again, I’ve tried the script:
// Define variables
var file = app.activeDocument;
var pages = file.pages;
var numberOfPages = pages.length;// Duplicate each page
for (i = 0; i < numberOfPages; i++) {
file.pages[i].duplicate();
}// Intertwine pages
for (var i = 0; i < (numberOfPages – 1); i++) {
var currentPage = (numberOfPages + i);
var insertPoint = (i + i);file.pages[currentPage].move(LocationOptions.AFTER, file.pages[insertPoint]);
}The only problem is that I get an syntax error
-
September 26, 2019 at 5:18 am #14324058Jelle Wouter VerschurenMember
Fixed it
-
September 26, 2019 at 5:50 am #14324057Lasse BrendenMember
There’s an en dash on line 12 that should have been a regular dash. See the updated script here:
https://gist.github.com/lassebrenden/ec2441937899b2458b0c4ffe6602a7de -
September 26, 2019 at 1:27 pm #14324046Aaron RussellMember
This is really great! Is there a way to have it only duplicate the selected page vs all pages in the document by any chance?
-
September 27, 2019 at 10:10 am #14324032Lasse BrendenMember
I ‘m not able to test if this actually works right now, but if you only need to duplicate the currently active page (i.e. not necessarily what you have selected), something like this might work:
var activeDocument = app.activeDocument
var activePage = app.activeWindow.activePageactivePage.duplicate()
activeDocument.pages[activeDocument.pages.length – 1].move(LocationOptions.AFTER, activePage)-
August 4, 2021 at 10:17 pm #14345481J DParticipant
I’m getting a syntax error on this too.
Would anyone have a tip on how to edit this script below to affect the current page only?
Thanks!
// Define variables
var file = app.activeDocument;
var pages = file.pages;
var numberOfPages = pages.length;// Duplicate each page
for (var i = 0; i < numberOfPages; i++) {
pages[i].duplicate();
}// Intertwine pages
for (var j = 0; j < (numberOfPages – 1); j++) {
var currentPage = (numberOfPages + j);
var insertPoint = (j + j);pages[currentPage].move(LocationOptions.AFTER, pages[insertPoint]);
}
-
-
September 27, 2019 at 1:13 pm #14324031Aaron RussellMember
Thanks for sending that. I’ve got to play the amateur card. I’m not entirely sure what to do with this code. I opened the other script in text edit & pasted this in, but was getting syntax error. Should I only replace part of the code with what you sent? Sorry to bug but I appreciate it.
-
September 28, 2019 at 10:20 am #14324021David BlatnerKeymaster
-
August 5, 2021 at 3:35 pm #14345506J DParticipant
Would anyone have a tip on how to edit this script below to affect the current page only?
Thanks!
// Define variables
var file = app.activeDocument;
var pages = file.pages;
var numberOfPages = pages.length;// Duplicate each page
for (var i = 0; i < numberOfPages; i++) {
pages[i].duplicate();
}// Intertwine pages
for (var j = 0; j < (numberOfPages – 1); j++) {
var currentPage = (numberOfPages + j);
var insertPoint = (j + j);pages[currentPage].move(LocationOptions.AFTER, pages[insertPoint]);
} -
August 24, 2021 at 3:57 pm #14346619J DParticipant
Hi all.
Old thread, but still trying to sort this out.
I’ve tried these two scripts, but they seem to also copy objects on the pasteboard of the pages below and above, resulting in the pasteboard expanding with unwanted objects.
Any suggestions for a way to edit these to create a script that:
• Duplicates the active page and/or spread
• Moves that page/spread to sit next in order
• Doesn’t affect pasteboard objects that it shouldn’tapp.activeWindow.activePage = app.layoutWindows[0].activePage.duplicate (LocationOptions.AFTER, app.layoutWindows[0].activePage);
or
app.activeWindow.activePage = app.activeWindow.activePage.duplicate (LocationOptions.AFTER, app.activeWindow.activePage);
-
-
AuthorPosts
- You must be logged in to reply to this topic.