Script to duplicate page/spread after current

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to duplicate page/spread after current

Viewing 26 reply threads
  • Author
    Posts
    • #63925

      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

    • #63939

      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.

      • #66202

        Hello,
        How do I use this “one-liner”, copy and paste it somewhere?

      • #66215

        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.”

      • #89332

        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!

    • #63951

      Works like a charm . . . keyboard shortcut and all. Thanks!

      • #68429
        gprovan
        Member

        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!

    • #70198
      gamouning
      Member

      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?

      • #114690

        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]);
        }

    • #70280
      Matt Mayerchak
      Participant

      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?

    • #70346

      Hi, i think you could try:
      app.layoutWindows[0].activePage.duplicate (LocationOptions.AT_BEGINNING, app.activeDocument.pages[0]);

    • #70632
      Matt Mayerchak
      Participant

      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

    • #71190

      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);
      }
      OR

      var 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

    • #83460

      Dear Jongware,

      can i use your code to duplicate the whole pages or the main document with reversed order

      Thanks in advance

    • #84293

      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.

    • #84294
      Matt Mayerchak
      Participant

      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!

    • #84344

      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

    • #84420

      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,

    • #89562

      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!

    • #89579

      You could use:

      app.activeWindow.activePage = app.activeDocument.pages [yourPageIndex];

      or

      app.activeWindow.activePage = app.activeDocument.pages.itemByName (“Your page number”);

      Michael

      • #89580

        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);

    • #89583

      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);

    • #114511
      Tamás Nagy
      Participant

      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

    • #114705
      Tamás Nagy
      Participant

      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

    • #14324059

      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

    • #14324058

      Fixed it

    • #14324057

      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

    • #14324046

      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?

    • #14324032

      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.activePage

      activePage.duplicate()
      activeDocument.pages[activeDocument.pages.length – 1].move(LocationOptions.AFTER, activePage)

      • #14345481
        J D
        Participant

        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]);
        }

    • #14324031

      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.

    • #14324021
      David Blatner
      Keymaster
    • #14345506
      J D
      Participant

      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]);
      }

    • #14346619
      J D
      Participant

      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’t

      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);

Viewing 26 reply threads
  • You must be logged in to reply to this topic.
>