4 Replies Latest reply on Jan 10, 2017 1:21 AM by Harbs.

    duplicate pages and move to another file

    jakec88782761 Level 2

           Hi all,


      I have a script which needs some work, any pointers or resources would be great!


      It works for short files but anything larger and the script stops as it starts adding the pages to the IDMl file instead of the "doc".


      I'm not sure why this is happening.


      //  Opens a folder browser dialog box, where you can choose a folder with the file you want to import.
      //  The chosen folder is stored inside 'targetFolder'.
      //  Note: In this script, files will be imported in the order they appear in the folder!


      //  Stores a reference to the currently open document into which we will import the files (this should be the English master).

      importTranslations ();
      // Stores the IDML files inside 'fileList'


      function importTranslations () { // Create a function which will import the files.
      var doc = app.activeDocument,
      targetFolder = Folder.selectDialog ("Select a folder with idml translations \r*files must be in the order you want them to be imported*\rFR\rDE\rIT\rES\rNO\rRU\rTR",undefined, "Select a folder"),
      fileList = [];

      if ( targetFolder!= null ) {
      var fileList = targetFolder.getFiles("*.idml");

      } else {

      for ( var i = 0; i < fileList.length; i++ ) { //  Loop through the IDML files inside 'fileList'.

          TL = fileList[i]; //    Store each file in a variable called 'TL'.

          app.open( TL ); //  open each file
               TLpages = app.activeDocument.pages.everyItem().getElements(); //   Store the open IDML file pages inside 'TLpages'.

                                                             for ( var j = 0; j <TLpages.length; j++ ) { //    Loop through the pages
                                                                      switch ( fileList[i] ) {
                                                                          case fileList[0]:
                                                                              TLpages[j].pageColor=UIColors.BLUE; // Apply page color attributes depending on the order of the files in the folder.
                                                                          case fileList[1]:
                                                                          case fileList[2]:
                                                                          case fileList[3]:
                                                                          case fileList[4]:
                                                                          case fileList[5]:
                                                                           case fileList[6]:
                          TLpages[j].duplicate(); //  Duplicate the pages
                          if ( doc.pages.length % 2 == 1 ) {  // Add a spread if the number of pages in the main doc is odd
                              doc.spreads.add( { allowPageShuffle: true } );  //Allow the pages to shuffle
                              TLpages[j].move( LocationOptions.AFTER, doc.pages[-1] );
                           } else { TLpages[j].move( LocationOptions.AFTER, doc.pages[-1] );  // If the number of pages is even then just move the pages to the end of the main doc.
                         if ( doc.sections.length > 1 ) { // If there are more than 1 section then remove the excess sections.
                              doc.sections.itemByRange(1, -1 ).remove();

           app.activeDocument.close(SaveOptions.NO);  // Close the IDML and don't save.

      var pages = app.activeDocument.pages.everyItem().getElements();

              for ( i = 0; i<pages.length; i++ ) {
                      if (pages[i].pageItems.length==0) {
          } //delete empty pages


      alert ("Files successfully imported", "Confirmation message");
      //=================================================END MAIN FUNCTION=================
      var pageChecker = (function () {
      var myDoc=app.activeDocument;
      mylength = myDoc.pages.length,
      mydiv = ( mylength % 4 ),
      myadd = ( 4 - mydiv ); 

      if ( mydiv == 0 ) { 
      alert( "No issue with Page count. Press OK to go next step." )

      if ( myadd != 4 ) { 
      var myconfirmation=confirm ( "Need to add " + myadd + " pages. Shall I add them?" );

      if ( myconfirmation ) { 

      if ( mydiv !=0 ) { 
          for ( i = 0; i < myadd; i++ ) {
          myDoc.pages.add( LocationOptions.BEFORE, myDoc.spreads[-1] ); 


      //======================insert an empty page to make the document pages a multiple of 4 (for booklets)
      var bm = (function () {
      var newDoc = app.activeDocument;

      var frame = newDoc.textFrames.everyItem().getElements();
      var f = frame.length;
             for ( var c = 0; c < f; c++ ) {
               var paras = frame[c].paragraphs.everyItem().getElements();
                      for ( p = 0; p < paras.length; p++ ) {
                              var langArray = [ "EN", "FR", "DE", "IT", "ES", "NO", "RU", "TR" ];
                                      for ( var i = 0; i < langArray.length; i++ ) {
                                              if ( paras[p].contents === langArray[i] ) {
                                              var a = app.activeDocument.hyperlinkTextDestinations.add( paras[p] ); 
                                              var bm = app.activeDocument.bookmarks.add( a );
                                                   bm.name = paras[p].contents;


        • 1. Re: duplicate pages and move to another file
          Trevorׅ Adobe Community Professional

          Hi Jake.


          It is hard to read your code. Without it being formatted it looks a real mess. Please use the advanced editor so you can apply a syntax to your code. I could well be that even after the formatting the code will look a real mess.


          I really hardly saw the code but I noticed that you are referring to:


          1) doc

          2) activeDocument

          3) myDoc


          Could be more I didn't look for more than 5 seconds.


          I don't know if it will make a difference but use only 2 references.

          doc and idmlDoc or whatever you want to call them. Do not make any references to activeDocument by itself, i.e. if doc is the activeDocument refer to the document as doc and not activeDocument.


          To define doc try using:

          doc = app.documents.itemByID(app.activeDocument.id);

          Instead of:

          doc = app.activeDocument;

          It could be that the activeDocument is switching and when you refer to activeDocument during the execution of the script the activeDocument is the IDML document, by having doc referring to the ID you are keeping it fixed.

          Not too sure it will solve your problem but you should give it a go.





          1 person found this helpful
          • 2. Re: duplicate pages and move to another file
            Jump_Over Level 5



            Supporting Trevor I suggest to set a var mDoc = app.open(TL) ==> to be sure you are reffering to a proper doc.

            Looks like another case is switch...case part. You may need to change it alike:

            switch(i) {

            case 0:


            case 1:





            1 person found this helpful
            • 3. Re: duplicate pages and move to another file
              jakec88782761 Level 2

              Thanks Trevor, that was a really useful explanation, it definitely works better using the id property!


              However, I think it is messing up on any document that gets a dialog pop up to tell me that links are missing.

              When this happens the pages get moved to the end of the IDML and not the activeDocument.


              I've tried using userInteractionLevel but it still pops up.

              • 4. Re: duplicate pages and move to another file
                Harbs. Level 6

                Additionally, it looks like you're looping forward and duplicating. The loop should probably be reversed.