Home > InDesign Scripts > QuickTip: Insert Text Variable to Text Frame

QuickTip: Insert Text Variable to Text Frame

Wanna insert text variable to text frame through scripting? No problem. It’s fast and easy! ;) Question about this showed up about one week ago on Adobe’s InDesign Scripting forum, and I was also curious how to do it. I don’t want to write much, so, let’s go down to business :D

Again, we have to face with two locations where InDesign stores text variables. If you want to find out more, check this post: How to retrieve Text Variables. Here we are going to use document text variables. Let’s say we have text frame selected, and we want to insert File Name variable to it. This is one-line code, and it’s not really safe :)

//
app.selection[0].texts[0].textVariableInstances.add({associatedTextVariable:"File Name"});
//

Better way is to address variable by it’s location. It can be also done in one line:

//
app.selection[0].texts[0].textVariableInstances.add({associatedTextVariable:app.activeDocument.textVariables.item("File Name")});
//

Or more:

var mySel = app.selection[0];
var myVar = app.activeDocument.textVariables.item("File Name"); 
mySel.texts[0].textVariableInstances.add({associatedTextVariable:myVar});

Add method also has to parameter, so we can add variable at the end of paragraph two like this:

var mySel = app.selection[0];
var myVar = app.activeDocument.textVariables.item("File Name"); 
mySel.paragraphs[1].textVariableInstances.add(LocationOptions.AT_END, {associatedTextVariable:myVar});

Also, we can use insertion point instead of texts:

var mySel = app.selection[0];
var myVar = app.activeDocument.textVariables.item("File Name"); 
mySel.insertionPoints.item(0).textVariableInstances.add({associatedTextVariable:myVar});

Once variable is inserted in text frame, we can retrieve value of inserted text variable. Remember, you can’t get variable value before inserting into text box.

var mySel = app.selection[0];
var myVarValue = mySel.textVariableInstances.item("File Name").resultText;

Last thing I want to mention is how to convert text variable in selected frame to editable text.

var mySel = app.selection[0];
var myVarValue = mySel .textVariableInstances.everyItem().convertToText();

And for the end, I created short script that shows how to filter text variable types, show them in dropdown box, and insert them into text frame.

That’s it!

Have fun! :D

var myDocVars = app.activeDocument.textVariables;
var myTextVars = Array();
for(var i = 0; i < myDocVars.length; i++)
  if(myDocVars[i].variableType != VariableTypes.XREF_PAGE_NUMBER_TYPE &&
  myDocVars[i].variableType != VariableTypes.XREF_CHAPTER_NUMBER_TYPE)
    myTextVars.push(myDocVars[i].name);

var win = new Window('dialog', 'Insert Text Variable');
with(win){
  win.Pnl = add('panel', undefined, 'Select Variable');
  win.Pnl.orientation = 'row';
  with(win.Pnl){
    win.Pnl.sText = add('statictext');
    win.Pnl.ddL = add('dropdownlist', undefined, myTextVars);
    win.Pnl.Btn = add('button', undefined, 'Insert');
  }
};
win.Pnl.ddL.selection = 0;
win.Pnl.Btn.onClick = function(){win.close(1);}
win.center();
var myWin = win.show();

if(myWin == true){
  var mySel = app.selection[0];
  var myVar = app.activeDocument.textVariables.item(String(win.Pnl.ddL.selection));
  mySel.texts[0].textVariableInstances.add({associatedTextVariable:myVar});
}
About these ads
  1. November 25, 2011 at 10:53

    Super ! So. Is it possible to convert text variables to text in all open documents ?

    It wil be very cool !

    Many thanks

    • December 14, 2011 at 16:13

      Thierry,
      take a look at my script called “Variable2TextConverter”.
      It can convert all variables in specific paragraph style to text.
      You can also add simple “for” look through opened document.

      Hope that helps.

  1. No trackbacks yet.

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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 36 other followers

%d bloggers like this: