4 Replies Latest reply on Mar 29, 2010 9:08 AM by JossHarris

    Data Merge help (cs3) (JS)



           I am a total beginner at scripting and am trying to write a script for a data merge job. The job requires that after the merge is complete, a new page based on 'C-Master' needs to be added after every page of the job. It is a merge for carrier sheets and the 'C-Master' is an advert page that is required to print on the reverse of each page. I have written the script to add the same amount of pages as the job (based on 'C-Master') but i cannot figure out a way of moving them in between the pages of the job (they are all added at the end of the job). Any help would be much appreciated. Here is the code i have written so far.


      var myPages = app.activeDocument.pages.length
      var ad = app.activeDocument
      for (var i = 0; i < myPages; i ++ )




        • 1. Re: Data Merge help (cs3) (JS)
          [Jongware] Most Valuable Participant

          Change the ad.pages.add() line -- add an "at" parameters and a reference to the page where you want to add it relative to:


          Page add ([at: LocationOptions=LocationOptions.UNKNOWN][, reference: {Page | Spread | MasterSpread | Document} ][, withProperties: Object])
          Creates a new page.

          The location of the new page relative to the reference object or within the document or spread. (Optional) (default: LocationOptions.UNKNOWN) (Optional)
          The reference object. Note: Required when the at parameter value specifies before or after. Can accept: Page, Spread, MasterSpread or Document. (Optional)
          withPropertiesObjectInitial values for properties of the new Page (Optional)


          -- so if your 'current' page is, for example, app.activeDocument.pages[5], you can add a page after it using


          myPage = ad.pages.add(LocationOptions.AFTER, ad.pages[5]);


          ... then use 'myPage' as the base for the rest:


          myPage.appliedMaster = ... (etc.)

          1 person found this helpful
          • 2. Re: Data Merge help (cs3) (JS)
            JossHarris Level 1

            Thanks for the help, ive now changed it to:


            var myPages = app.activeDocument.pages.length
            var ad = app.activeDocument
            for (var i = 0; i < myPages; i ++ )
            var myPage = ad.pages.add(LocationOptions.AFTER, ad.pages[0])


            This is better as it adds the pages after page 1 but they are all added together after page 1. Im trying to add them alternatly so the pages go A,C,A,C,A,C etc

            • 3. Re: Data Merge help (cs3) (JS)
              [Jongware] Most Valuable Participant

              Yes, that's correct. The script does exactly what you tell it to: add each of the pages right after pages[0], which is the first one.


              To add one page after each existing one, you increment the add-after page counter by two (not by one, because you already added another page). Because this may get messy, it's more logical to start at the very end of the document and work your way back to the start:



              var myPages = app.activeDocument.pages.length;
              var ad = app.activeDocument;
              for (var i = myPages-1; i>=0; i-- )
                var myPage = ad.pages.add(LocationOptions.AFTER, ad.pages[i]);
                myPage.appliedMaster= ad.masterSpreads.item('C-Master');


              (BTW: It's highly recommended to end each command line with a semi-colon; it may catch some JS syntax errors that may creep in your scripts when JS thinks the command continues on the next line when you didn't really meant to do that.


              "for", "do", "while", and "if" lines don't 'need' the semi-colon (and never should have it either!), because technically what follows is part of that same line.

              The curly braces don't need a semi-colon because they are command delimiters of their own.)

              • 4. Re: Data Merge help (cs3) (JS)
                JossHarris Level 1

                Thats brilliant , works like a dream. Thanks for your help , you've made this a lot clearer for me!