Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bulk Import Text Variables into InDesign
/* ***************************************************************** */
/* Compiled: 09.16.2020 by Ben Owen for
/* Tested in InDesign 2020 (15.1.1)
/* Most of the code came from the InDesign script FindChangeByList.jsx
/* and the function setCustomTextVariable() by Jon S. Winters.
/* This script allows you to import a simple text file containing a
/* text variable list and registers them as "Custom Text Variables,"
/* this script doesn't work for any other kind of InDesign variable.
/* It will update any variables that already exist.
/* Make a text file with a new line for each variable you like to add.
/* Each line chould start with var, have a tab, then the variable name,
/* another tab and the variable value. Install the script in the proper
/* location, then run it from the Scripts panel. You'll be presented
/* a dialog box to choose your text file, choose the file and the
/* script will do the rest.
/* ***************************************************************** */
var doc = app.activeDocument;
var myVars = doc.textVariables;
var variableNames = myVars.everyItem().name;
var myFindChangeFile;
var myFindChangeArray, myFindPreferences, myChangePreferences;
myFindChangeFile = myFindFile("./Test.ini")
if(myFindChangeFile != null){
myFindChangeFile = File(myFindChangeFile);
var myResult ="r", undefined, undefined);
if(myResult == true){
//Loop through the find/change operations.
myLine = myFindChangeFile.readln();
//Ignore comment lines and blank lines.
myFindChangeArray = myLine.split("\t");
setCustomTextVariable(doc, myFindChangeArray[1], myFindChangeArray[2])
} while(myFindChangeFile.eof == false);
function myFindFile(myFilePath){
var myScriptFile = myGetScriptPath();
var myScriptFile = File(myScriptFile);
var myScriptFolder = myScriptFile.path;
myFilePath = myScriptFolder + myFilePath;
if(File(myFilePath).exists == false){
//Display a dialog.
myFilePath = File.openDialog("Choose the file containing your find/change list");
return myFilePath;
function myGetScriptPath(){
myFile = app.activeScript;
myFile = myError.fileName;
return myFile;
function setCustomTextVariable(docRef, name, value) {
//-- Written: 2010.04.19 by Jon S. Winters of electronic publishing support
try {
var atv = docRef.textVariables.everyItem();
if (atv.hasOwnProperty(name)) {
atv[name].variableOptions.contents = value;
else {
var newTV = docRef.textVariables.add({ name: name, variableType: VariableTypes.CUSTOM_TEXT_TYPE });
newTV.variableOptions.contents = value;
return true;
catch (failSilently) {
var localError = failSilently;
return false;
// TextVariables.txt
// A support file for ImportVariables.jsx
// Eachline that will be imported must start with var, then have one tab character,
// the desired variable name, one more tab and the value for the name.
var DFASKHJ Nullam quis risus eget urna mollis ornare vel eu leo.
var OFHISD Vestibulum id ligula porta felis euismod semper.
var IOWUE Sedusce dapibus, tellus ac cursus commodo, ut fermentum massa justo sit amet risus.
var OSVDU Sed tortor mauris condimentum nibh, posuere consectetur est at lobortis.
var ADVIO Aenean lacinia bibendum nulla sed consectetur. Sed posuere consectetur est at lobortis.
var ADHIO Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.
var ETWUOP Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Donec sed odio dui.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment