• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Add TextVariable to TextFrame

Community Beginner ,
May 16, 2018 May 16, 2018

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

Can you point me to one of those examples?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

Sure :

Capture d’écran 2018-05-17 à 17.32.07.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

Hi,

Jongware already pointed to textVariableInstances with their method add().

See ExtendScript DOM documentation by Jongware here:

Adobe InDesign CS6 (8.0) Object Model JS: TextVariableInstance

Adobe InDesign CS6 (8.0) Object Model JS: TextVariableInstances

Adobe InDesign CS6 (8.0) Object Model JS: TextVariable

Adobe InDesign CS6 (8.0) Object Model JS: TextVariables

Also inspect this cornucopia of documentation also by Jongware:

Indesign JavaScript Help

Best,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines