Home > InDesign Scripts > How to retrieve Text Variables

How to retrieve Text Variables

We are going to look at simple way to get all text variables. Important thing is that InDesign had two different lists of text variables. One list is stored in ‘app’ object, and another is in ‘document’ object. This is very important to remember! Part of this script was published on Adobe’s InDesign scripting forum by Peter Kahrel. So, let’s get started.

Well, what’s the difference between ‘app’ and ‘document’ variables? If we have no document opened, and we create new text variable through script or InDesign-> Type-> Text Variables -> Define…, created variable will be ONLY visible and available in NEW created documents! ‘Document’ variables, like the name says, are stored inside active document. There is very good option to load variables from one document to another or from document to ‘app’ through InDesign UI InDesign-> Type-> Text Variables-> Define…-> Load. Also, ‘document’ variable has two additional items that are built in document: XRefChapterNumber and XRefPageNumber and you can’t change them.

Good, let’s see how to retrieve ‘app’ and ‘document’ variables.

var myDocVariables = app.activeDocument.textVariables;
var myAppVariables = app.textVariables;

So, now we can now count variables or retrieve variable names like this:

var myDocVariablesCount = myDocVariables.length;
var myDocVariablesNames = myDocVariables.everyItem().name;

Also, very good thing is that we can determinate variable type like this:

var myDocVariablesType = myDocVariables.everyItem().variableType;

Maybe in some future posts, we will go through adding text variable to document. Ok, so, let’s now look at short snippet for retrieving document text variables. As I said, this is modified script from Adobe’s forum. Script is going to create small table with 3 columns for index, name and variable type.

if(app.documents.length > 0){
  var myDoc = app.activeDocument;
  var myVariables = myDoc.textVariables;
  var myVariablesTable = ["Index","Name","Type"];
  var rowCount = 1;

  for(var i = 0; i < myVariables.length; i++){

  var myTextFrame = myDoc.pages[0].textFrames.add();
  myTextFrame.geometricBounds = [20,20,250,300];

  var myVariablesTbl = myTextFrame.insertionPoints[0].tables.add();
  myVariablesTbl.columnCount = 3;
  myVariablesTbl.bodyRowCount = rowCount;
  myVariablesTbl.contents = myVariablesTable;

One more thing you can do is to filter variable types like this (just alter original script):

for(var i = 0; i < myVariables.length; i++){
  if(myVariables[i].variableType == VariableTypes.CUSTOM_TEXT_TYPE){

That’s all for now!

Have fun! 😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: