Assign data merge tag automatically using a script

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Assign data merge tag automatically using a script

Tagged: ,

Viewing 11 reply threads
  • Author
    Posts
    • #88563

      Anybody knows of a script that would take a text snippet matching data merge layout <<Name>> and assign it to its reference in the data merge pane ?

      I mean the designer inputted the tags as text in the layout and I want to grab that <<tag1>>, <<tag2>>, etc. and make it behave as if I had selected it and search through a long list of tag the right one and then clicked on it.

      I would be very grateful is such thing existed.

    • #88600
      Loic Aigon
      Member

      Here is a proposal

      var main = function() {
      var doc = app.properties.activeDocument,
      found, n, dm, tagName, ip, st, df;

      if (!doc ) return;

      app.findGrepPreferences = null;
      app.findGrepPreferences.findWhat = “<<[^>]+>>”;

      found = doc.findGrep();
      n = found.length;

      while ( n– ) {
      tagName = String(found[n].contents.replace(/(<<|>>)/g, “”));
      st = found[n].parentStory;
      ix = found[n].index-1;
      df = getField (doc, tagName );

      if ( df!=null ) {
      found[n].remove();
      doc.dataMergeTextPlaceholders.add( st, ix, df );
      }
      }
      }

      function getField ( doc, tagName ) {
      var dfs = doc.dataMergeProperties.dataMergeFields,
      n = dfs.length, df;

      while ( n– ) {
      df = dfs[n];
      if ( df.fieldName==tagName) return df;
      }

      return null;

      }

      var u;
      app.doScript ( “main()”, u,u, UndoModes.ENTIRE_SCRIPT, “Place tags” );

      HTH

      Loic
      http://www.ozalto.com

    • #88601

      Hi Loic,

      Thank you very much for your help.
      I tried the script and it run without error but it does not produce any result… but I think I know why.

      My tags are like this <<19,6,0,0,0>> could it be that it just doesn’t find any of those ?

    • #88602
      Loic Aigon
      Member

      Hello,

      if you type in “<<19,6,0,0,0>>” then teh script will look for a possible datamerge field named “19,6,0,0,0”. Do your datamerge source contain such a field ?

      feel free to send a screenshot.

    • #88606

      That is how it is setup.

      Not sure wow to upload an image, here a screenshot in dropbox.
      https://www.dropbox.com/s/ccr2sb7kxim6dpc/Screen%20Shot%202016-09-22%20at%2011.56.27%20AM.png?dl=0

      The text is selectable before and after the script is played.. and the data merge reference is about at the same height on the left.

      Regards

    • #88608
      Loic Aigon
      Member

      It works here so I guess there are encoding peculiarities. Feel free to send me your txt source file through my website contact page so I can give this a closer look.

      Loic
      http://www.ozalto.com

    • #88611
      David Blatner
      Keymaster

      You might also ensure that the script does work on a simple merge tag in a new document. For example just try “name” and see if that works. I’ve never seen such crazy data merge field names! :-)

    • #88616

      Hi Loic,

      Your website form give me an error when I try to send the message.
      I guess it’s because I’m trying to send a link… but I can’t seems to find a way to attach a file (which should be utf-8).

      @David, these are product numbers they represent a whole lot of information in each segment. Categories, Sections, Rows, etc.

      Even if I do a new empty document it doesn’t work, is there some error reporting I could add to the script ?

      Regards

    • #88617
      Loic Aigon
      Member

      hi @sebastien, try my email [my first name] dot [my last name] at my website ;)

    • #88619
      Loic Aigon
      Member

      Hi @sebastien,

      I can totally make the script working with your file. May I have misunderstood the initial requirement of typing the tag name as regular text inside the doc ?

      I mean “<<1,0,0,0,0>>” as text that once located will place the “1,0,0,0,0” tag instead of the text ?

      Loic

    • #88643

      Hi Loic,

      I’ve managed to understand what I did wrong… but I don’t totally understand why it behave like this.
      The script does work just as intended. When the Data merge panel is shown I can see that they are applied, and when I try to select a part of a tag it select as a whole.

      What I was doing wrong is thinking that once applied as a tag it would always be selectable as a whole.. but when the Data merge panel isn’t shown on screen tags are still selectable as bare text. I was looking at the script panel and selecting the text to see if it worked… which wouldn’t happened until I had both the script panel and data merge panel opened side by side and thus shown on the screen. Then it magically worked and select as I thought it would.

      Again, you’re help was and is very precious.

    • #105165

      This is great however if the variable is in a table cell it removes it from the cell and add’s it above the table. Is there a way to prevent this and get it to add it back in the original cell?

Viewing 11 reply threads
  • You must be logged in to reply to this topic.
>