2 Replies Latest reply on Jul 9, 2015 7:18 AM by Colin Flashman

    IDCS6 MACOSX JS: Data Merging many txt files one after another

    Colin Flashman Adobe Community Professional

      Hello everybody.


      I have a situation where I have to data-merge many text files to the one actual indesign "base" file. The formats of the databases are all the same, the point of difference is a code in the second field of the database that refers to a mailing zone (not a zip/postcode, a "distribution centre" code that has 56 or so possibilities). I would prefer to data merge one file and then somehow split the resulting PDF via Acrobat, but the length of the resulting mailing zones is inconsistent and something that can't be done though the "split document" feature in acrobat.


      The database starts out as one massive file but using a one-line  code, can be split into its mailing zones into separate text files.


      My ultimate question: is there any way to data merge more than one file at once (e.g. one after another) and give them their names based on the names of the input text file?


      Ole Kvern had a script to Data Merge one file without the UI. I have altered the last line slightly so that it outputs to a PDF based on [High Quality Setting]:



      if(app.documents.length != 0){
              var myDocument = app.activeDocument;
              app.dataMergeOptions.removeBlankLines = true;
              //Select a source file.
              var myDataFile = File.openDialog("Select a data file")
              var exported = "file location(redacted for the sake of this post)"
              if(myDataFile != ""){
                      myDocument.dataMergeProperties.exportFile(exported,"[High Quality Print]",);


      I figure the answer has to do with the variable "myDataFile" and rather than being simply open a dialog, it would be somehow selecting an array of text files, but this is where I am out of my depth.


      Any insights or other thoughts that may not involve trying to do many merges at once but may somehow revolve around one large merged PDF?


      Many thanks



        • 1. Re: IDCS6 MACOSX JS: Data Merging many txt files one after another
          Colin Flashman Adobe Community Professional

          I've answered my own question, but the answer lies outside of Adobe InDesign – it uses bookmarks within Adobe Acrobat.


          1) Data Merge the art/data to one large PDF file.

          2) In the resulting PDF file, manually search out the mailing zones and apply bookmarks to each first instance only of each mailing zone. In this case, it is a manual process of find/replace to find one of 70 possible mailing zones that may be used in any database. Each bookmark should be named with its respective code. It is a manual process, but it would take 5 minutes so this is acceptable.

          3) Using "Split Document" from the pages panel, make sure the "top level bookmarks" radio button is checked, and in the Output Options make sure that the "Use bookmark names for files" radio button is checked.


          I'm aware that this was a rather obscure question that only a fraction of users on this forum may wish to know an answer for, but hopefully this workaround helps someone else in a similar situation.



          • 2. Re: IDCS6 MACOSX JS: Data Merging many txt files one after another
            Colin Flashman Adobe Community Professional

            Since writing my last answer, i've realised there was a better answer. It involves a script by Peter Kahrel and a modified version of a script by Olav Kvern. Full details can be found here: Batch Data Merge | Adobe Community


            I've since made further mods to the script so it is easier to understand:


            if(app.documents.length != 0){
            // make sure to use the document that is open 
                    var myDoc = app.activeDocument;
            // get the name of the document that is open for future naming purposes
                    var myName = myDoc.name
            // get the location of the folder that contains both the indesign files and the text files
            // define the names of the data files to use based upon the names of the indesign files
                    var myImport = myLocation + myName + ".txt"
            // define the names of the data merged PDFs to be created
                    var myExport = myLocation + myName + ".pdf"
            // the actual engine for the merge, including DM instructions such as remove blank lines, object fitting etc.
                    var myDataFile = myImport
                    app.dataMergeOptions.removeBlankLines = true;
                    if(myDataFile != ""){
                            myDoc.dataMergeProperties.exportFile(myExport, "[PDF/X-1a:2001]", );


            So this script will automatically merge a file that is open PROVIDED that the data file is the same name as the indesign file, only with .txt at the end e.g.

            woodward.indd (indesign file)

            woodward.indd.txt (text file used for the merge)


            This script still does not fulfil the original brief, and that was using one indesign document, to loop through many text files and merge to PDF based on the text files.


            HOWEVER, i'm sure the answer to that relies solely on using the above script (no need for Peter's batch convert script) and changing line 9 to be a for-loop... but again this is where I come to a grinding halt. Any help would be appreciated.