6 Replies Latest reply on Jul 6, 2009 1:46 AM by MrFredHale

    [JS] IDCS3 – Beginner having problems with dataMerge


      I'm a designer trying to get to grips with writing a data merge script, but am struggling with data merge placeholders, and how they relate to text fields. I've looked around for tutorials etc relating to scripting the data merge function but have not been able to find much information. I'd be grateful if anyone could point out where I'm going wrong. The script returns an "Object is Invalid" error on the highlighted line.


      Below is the section of the script relating to the text fields. I am sorry for its inelegance; I am very much in the early stages of learning both JavaScript and scripting InDesign. Many thanks.



      // insert entry text frame
      var myEntry = myDocument.pages.item(0).textFrames.add();
      myEntry.label = "Entry";
          with (myEntry) {
              geometricBounds = ["9mm", "100mm", "139mm", "5mm"];
              fillColor = "Black75";
              textFramePreferences.verticalJustification = 1667591796;
              textFramePreferences.insetSpacing = "5mm";
      // myEntry.paragraphs.item(0).applyParagraphStyle(myParagraphStyleEntry, true);

      var myEntryStory = myEntry.parentStory;
      var myStoryOffset = myEntry.lines.item(0).insertionPoints.item(0).index;
      var myDataMergeProperties= myDocument.dataMergeProperties;
      var myEntryPlaceholder = myDocument.dataMergeTextPlaceholders.add(myEntryStory, myDataMergeProperties.dataMergeFields.item(0));

      // insert date/time text frame
      var myDateTime = myDocument.pages.item(0).textFrames.add();
      myDateTime.label = "Date & Time Field";
          with (myDateTime) {
              geometricBounds = ["4mm", "100mm", "6mm", "70mm"];
              contents = "00:00 00.00.00";   
      myDateTime.paragraphs.item(0).applyParagraphStyle(myParagraphStyleHeaderRight, true);


      var myDateTimeStory = myDateTime.parentStory;
      var myStoryOffset = myDateTime.lines.item(0).insertionPoints.item(0).index;
      var myDataMergeProperties= myDocument.dataMergeProperties;
      var myDateTimePlaceholder = myDocument.dataMergeTextPlaceholders.add(myDateTimeStory, myDataMergeProperties.dataMergeFields.item(0));

      // insert entry no. text frame
      var myEntryNo = myDocument.pages.item(0).textFrames.add();
      myEntryNo.label = "Entry Number";
          with (myEntryNo) {
              geometricBounds = ["142mm", "85mm", "144mm", "100mm"];
              contents = "E–000";
              textFramePreferences.verticalJustification = 1651471469;
      myEntryNo.paragraphs.item(0).applyParagraphStyle(myParagraphStyleHeaderRight, true);


      var myEntryNoStory = myEntryNo.parentStory;
      var myStoryOffset = myEntryNo.lines.item(0).insertionPoints.item(0).index;
      var myDataMergeProperties= myDocument.dataMergeProperties;
      var myEntryNoPlaceholder = myDocument.dataMergeTextPlaceholders.add(myEntryNoStory, myDataMergeProperties.dataMergeFields.item(0));

        • 1. Re: [JS] IDCS3 – Beginner having problems with dataMerge

          I'm new to scripting too; maybe that's why I'm confused by your post. Why is it that you have to write a script to use datamerge?


          Here's a helpful tutorial on using the datamerge pallette in the GUI; maybe this will offer some insight?



          • 2. Re: [JS] IDCS3 – Beginner having problems with dataMerge
            MrFredHale Level 1

            Thanks for your reply marykulp.


            The script I'm trying to write checks to see if an external file has been updated, and, if so, creates a document with the new content before printing it – all without user intervention. I have a feeling I'm probably not going about it the right way though! Many apologies for not being clearer in my original post.



            • 3. Re: [JS] IDCS3 – Beginner having problems with dataMerge
              Kasyan Servetsky Level 5

              var myStoryOffset = myEntry.lines.item(0).insertionPoints.item(0).index;

              You are getting the error because you are trying to access non-existent first line. If you check myEntryStory.lines.length, you'll get Result: 0.

              If you want to get the first insertion point of the text frame use var myStoryOffset = myEntry.insertionPoints.item(0).index; instead.


              However, from the snippet you posted I can't see for what you need it: you define it 3 times but never use it.



              1 person found this helpful
              • 4. Re: [JS] IDCS3 – Beginner having problems with dataMerge
                MrFredHale Level 1

                Thanks Kasyan, that's really helpful in understanding why I was getting that error.


                To try to understand dataMerge I've been looking at this post, which shows a script that marks up a text frame with data placeholders and then imports a data file. I don't think I'm interpreting it correctly with regard to the script I'm trying struggling to write though – I have three data elements in the data file, which I would like to place in three separate text frames.

                • 5. Re: [JS] IDCS3 – Beginner having problems with dataMerge
                  Kasyan Servetsky Level 5

                  Here is a modified version of the Ole's script that demonstrates how to place three data elements into three separate text frames:


                  //Create an example data file.
                  var myTextFile = new File("~/Desktop/Data Merge Test.txt");
                  //If the user clicked the Cancel button, the result is null.
                  if(myTextFile != null){
                      //Open the file with write access.
                      myString = "Name\tAddress\tCity\r";
                      for(var myCounter = 0;myCounter< 20; myCounter++){
                          myString += "Record: " + myCounter + ", Field:" + 1 + "\t";
                          myString += "Record: " + myCounter + ", Field:" + 2 + "\t";
                          myString += "Record: " + myCounter + ", Field:" + 3 + "\r";


                  var myDocument = app.documents.add();
                  myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
                  myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
                  var myPage = myDocument.pages.item(0);


                  var myNameTextFrame = myPage.textFrames.add({geometricBounds:[36, 36, 252, 252], contents:"Name:"});
                  var myAddressTextFrame = myPage.textFrames.add({geometricBounds:[288, 36, 504, 252], contents:"Address:"});
                  var myCityTextFrame = myPage.textFrames.add({geometricBounds:[540, 36, 756, 252], contents:"City:"});


                  var myDataMergeProperties= myDocument.dataMergeProperties;


                  var myStory = myNameTextFrame.parentStory;
                  var myStoryOffset = myNameTextFrame.lines.item(0).insertionPoints.item(-1);
                  var myNamePlaceHolder = myDocument.dataMergeTextPlaceholders.add(myStory, myStoryOffset, myDataMergeProperties.dataMergeFields.item(0));
                  myStory = myAddressTextFrame.parentStory;
                  myStoryOffset = myAddressTextFrame.lines.item(0).insertionPoints.item(-1);
                  var myAddressPlaceHolder = myDocument.dataMergeTextPlaceholders.add(myStory, myStoryOffset, myDataMergeProperties.dataMergeFields.item(1));
                  myStory = myCityTextFrame.parentStory;
                  myStoryOffset = myCityTextFrame.lines.item(0).insertionPoints.item(-1);
                  var myCityPlaceHolder = myDocument.dataMergeTextPlaceholders.add(myStory, myStoryOffset, myDataMergeProperties.dataMergeFields.item(2));

                  • 6. Re: [JS] IDCS3 – Beginner having problems with dataMerge
                    MrFredHale Level 1

                    Thank you Kasyan, the example you posted has helped immensely; I have a better understanding of the data merge object now & have managed to get my script working.