Coming back to scripted text variables, here's a script that illustrates a couple of things that were raised in this thread. I use it to display and set all text variables in a document. I use it to set variables in documents I use as masters for journals, to set things like volume, issue, year, date, and whatever else.
When first run, the script creates a two-column table on the first page of the document. It places variable names in the first column, their values in the second column. To set different values for any variable, replace the value in the second column with the new value. Then run the script again.
Peter
(PS: jongware's list is a great tool indeed, should have mentioned that)
doc = app.activeDocument;
if ( doc.textFrames.item ('vars___') == null)
show_variables (doc);
else
set_variables (doc);
function show_variables (doc)
{
var v = doc.textVariables;
var table_contents = [];
var n = 0;
for (var i = 0; i < v.length; i++)
{
if (v.variableType == VariableTypes.customTextType)
{
table_contents.push (v.name);
table_contents.push (v.variableOptions.contents);
n++;
}
}
var tf = doc.pages[0].textFrames.add ({label: 'vars___'});
tf.geometricBounds = [20,20,500,500];
var table = tf.insertionPoints[0].tables.add();
table.columnCount = 2;
table.bodyRowCount = n;
table.contents = table_contents;
}
function set_variables (doc)
{
var t = doc.textFrames.item ('vars___').parentStory.tables[0];
var var_names = t.columns[0].contents;
var new_contents = t.columns[1].contents;
var vars = doc.textVariables;
for( var i = 0; i < var_names.length; i++)
vars.item (var_names).variableOptions.contents = new_contents;
// doc.textFrames.item ('vars___').remove()
}
... View more