Reply To: Assign data merge tag automatically using a script

#88600
Anonymous
Inactive

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

This article was last modified on September 22, 2016

Comments (0)

Loading comments...