Skip to main content
Participating Frequently
May 17, 2018
Question

Add TextVariable to TextFrame

  • May 17, 2018
  • 2 replies
  • 1631 views

How do I add a TextVariable to a TextFrame in InDesign?

I am creating the TextVariable like this.

var textVariable = app.activeDocument.textVariables.add({name: "My Name"});

I'm coming from Illustrator scripting where you have a content variable attached to each text frame object. I'm confused on how to link these text variables to text frames in InDesign.

This topic has been closed for replies.

2 replies

Jongware
Community Expert
Community Expert
May 17, 2018

It is complicated – but that's because InDesign's text model is much more complicated than Illustrator's.

textVarR = app.selection[0].parentStory.textVariableInstances.add(LocationOptions.AFTER, app.selection[0]);

textVarR.associatedTextVariable = app.activeDocument.textVariables.item("My Name");

You insert a ("any", really) text variable code in your text, and then you associate it with the particular one you want.

Other than that, inserting the text variable in the proper place is pretty much the same as inserting text. You must provide a reference parent object – a plain text frame would not work, it needs a "text'-like object such as 'yourTextFrame.parentStory' – and "where" you want it to be inserted inside that parent object ("at the start", "at the end", or "before" or "after" a particular indexed element).

Participating Frequently
May 17, 2018

I tried your example but this is the error I get when I run your first line in ExtendScript with the text frame selected. I don't understand the error because it says TextFrame is an expected value.

Error: Invalid value for parameter 'reference' of method 'add'. Expected TextVariableInstance, TextFrame, EndnoteTextFrame, Story, XmlStory, Note, Cell, Footnote, Table, TextColumn, TextStyleRange, Text, InsertionPoint, Paragraph, Line, Word, Character or Change, but received TextFrame.

Loic.Aigon
Legend
May 17, 2018

Search inside forum, pretty certain there are dozens of samples.

Participating Frequently
May 17, 2018

Can you point me to one of those examples?

Loic.Aigon
Legend
May 17, 2018

Sure :