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: Data Merge, script
- This topic has 11 replies, 4 voices, and was last updated 5 years, 7 months ago by Scott Williams.
-
AuthorPosts
-
-
September 21, 2016 at 1:51 pm #88563Sebastien GauthierMember
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.
-
September 22, 2016 at 8:04 am #88600Loic AigonMember
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
-
September 22, 2016 at 8:41 am #88601Sebastien GauthierMember
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 ?
-
September 22, 2016 at 8:47 am #88602Loic AigonMember
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.
-
September 22, 2016 at 9:04 am #88606Sebastien GauthierMember
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=0The 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
-
September 22, 2016 at 9:13 am #88608Loic AigonMember
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.
-
September 22, 2016 at 9:46 am #88611David BlatnerKeymaster
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! :-)
-
September 22, 2016 at 10:47 am #88616Sebastien GauthierMember
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
-
September 22, 2016 at 11:47 am #88617Loic AigonMember
hi @sebastien, try my email [my first name] dot [my last name] at my website ;)
-
September 22, 2016 at 12:48 pm #88619Loic AigonMember
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
-
September 25, 2016 at 7:01 pm #88643Sebastien GauthierMember
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.
-
August 2, 2018 at 6:25 am #105165Scott WilliamsMember
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?
-
-
AuthorPosts
- You must be logged in to reply to this topic.