3 Replies Latest reply on Aug 10, 2017 10:45 AM by clemdia

    Data Merge: Automatically Place Text from File?

    clemdia Level 1

      I'm doing a data merge for a large catalog, and I'm placing images with the "special" image column (i.e., @image1, @image2, ...) - all works well.


      However the text for each page is actually in a text file, and contains line breaks, tabs, etc. Is there an equivalent to the special image tag - or a scripted solution - to automatically place text from a text file as specified in each row of the data file?


      My data file is something like this:


        • 1. Re: Data Merge: Automatically Place Text from File?
          amaarora Adobe Employee


          I could not think of a way to do this.


          I suggest you use Data merge with this file normally. This will add description field as normal text into the text frames. You can then use InDesign script to scan through all frames and then place the contents of filename.txt in the frame that has the text filename.txt in it.


          I hope that helps.



          1 person found this helpful
          • 2. Re: Data Merge: Automatically Place Text from File?
            clemdia Level 1

            This appears to me to be the only way to go about it - thanks for the comment. If I'm successful, I'll post the script when I'm done.


            I'm thinking I'll go about it this way:

            1. Merge and place the path to the file in each text frame with a "placeholder" style applied (e.g., a style called "Replace Me").

            2. Post-merge with JSX, find all text frames in the document with the "Replace Me" style on the first paragraph, and use the contents of the text frame (which would be the full path to the text file) to open the file and replace all text in the frame.

            3. Finally, re-style the text as needed using JSX.


            Sure seems like an oversight to me that you can't merge text from external files (you can only place images) but I guess if many people needed to do things this way the capability would already exist, right?

            1 person found this helpful
            • 3. Re: Data Merge: Automatically Place Text from File?
              clemdia Level 1

              Here's a quick-and-dirty script using various bits I found on these forums plus some of my own work. The script takes advantage of Script Labels to flag all text frames in a document where text import from file is desired. It also applies a style of your choosing to all paragraphs in the text frame after the file text is imported.


              Use at your own peril, of course - no warranty implied:


              1. Add a Script Label to the targeted text frame on your page master.
              2. Do your data merge as normal, including the full path to your text file merged into that labelled text frame. IMPORTANT: put no other content in that text frame!!!
              3. Run this script:


              var myLabel = "SomeScriptLabel"; // Script label of frames you want to process (see Step 1)
              var myStyle = "SomeParagraphStyle"; // Name of style you want applied
              var document = app.documents.item(0);
              var allTextFrames = toArray(document.textFrames);
              var textFrames = selectWhere(myLabel, "label", allTextFrames);
              var i = textFrames.length;
              var attemptCounter = 0;
              var successCounter = 0;
              while (i--) {
                  if (textFrames[i].parentPage.appliedMaster) { // ignores master pages, as "appliedMaster" property will be null
                      var myStory = textFrames[i].parentStory;
                      var newText = getFileContents(myStory.contents);
                      if (newText !== false) {
                          newText = newText.replace(/\n/g,"\r"); // OPTIONAL: replaces soft line breaks with "real" line breaks
                          myStory.contents = newText;
              alert(successCounter + " of " + attemptCounter + " text frames updated");
              function selectWhere(value, key, array){
                  var i = array.length; var t; var filtered = [];
                          t = array[i];
                          if(t && t[key] == value){
                  return filtered;
              function toArray(objects){
                  var i = objects.length; var array = [];
                  return array;
              function getFileContents(filePath) {
                  filePath = filePath.split("\r");
                  var myTextFile = File(filePath[0]); // use only first line of frame contents as path
                  if (myTextFile.exists) {
                      myTextFile.open('r', undefined, undefined);
                      if (myTextFile !== '' && myTextFile !== null) {
                          var thisFileText = myTextFile.read();
                          return thisFileText;
                  return false;
              function updateStyles(myStory) {
                  for (var i=0; i < myStory.paragraphs.length; i++) {
                      var myParagraph = myStory.paragraphs[i];
                      myParagraph.appliedParagraphStyle = myStyle;
              2 people found this helpful