Skip to content

Instantly share code, notes, and snippets.

@altercation
Forked from circleb/ImportVariables.jsx
Created June 30, 2021 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altercation/5025c71e176b6c2a5dcde395bd7b9f3f to your computer and use it in GitHub Desktop.
Save altercation/5025c71e176b6c2a5dcde395bd7b9f3f to your computer and use it in GitHub Desktop.
Bulk Import Text Variables into InDesign
/* ***************************************************************** */
/* Compiled: 09.16.2020 by Ben Owen for 1torial.com
/* Tested in InDesign 2020 (15.1.1)
/* ben@benowen.net
/* Most of the code came from the InDesign script FindChangeByList.jsx
/* and the function setCustomTextVariable() by Jon S. Winters.
/*
/* DESCRIPTION
/* 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.
/*
/* INSTRUCTIONS
/* 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 = myFindChangeFile.open("r", undefined, undefined);
if(myResult == true){
//Loop through the find/change operations.
do{
myLine = myFindChangeFile.readln();
//Ignore comment lines and blank lines.
if((myLine.substring(0,3)=="var")){
myFindChangeArray = myLine.split("\t");
setCustomTextVariable(doc, myFindChangeArray[1], myFindChangeArray[2])
}
} while(myFindChangeFile.eof == false);
myFindChangeFile.close();
}
}
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(){
try{
myFile = app.activeScript;
}
catch(myError){
myFile = myError.fileName;
}
return myFile;
}
function setCustomTextVariable(docRef, name, value) {
//-- Written: 2010.04.19 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
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