BUG: InDesign ExtendScript - textVariableInstances.add always inserts first variable
When using ExtendScript to insert a TextVariableInstance with a specific textVariable, InDesign always inserts the first variable in the document (index 0), regardless of the provided textVariable argument. This happens even if a dummy instance is created and removed as a workaround. The bug occurs in new documents and affects all variable types.
Steps to reproduce:
1. Create a new document.
2. Add multiple text variables (e.g. 'Dateiname', 'Bildname', etc.).
3. Use ExtendScript to insert a TextVariableInstance with a specific variable:
insertionPoint.textVariableInstances.add({ textVariable: desiredVariable });
4. Check the inserted instance: It always references the first variable in the document.
Expected:
The inserted TextVariableInstance should reference the variable passed in the textVariable argument.
Actual:
The inserted instance always references the first variable (index 0).
Workarounds:
- Creating and removing a dummy instance does not solve the issue.
- Manual insertion via UI works as expected.
Environment:
- InDesign version: 26.2, 25.0,
- OS: 26.2
// InDesign ExtendScript: Create new A4 page, text frame, insert all text variables
if (app.documents.length === 0) {
var doc = app.documents.add();
} else {
var doc = app.activeDocument;
}
doc.documentPreferences.pageWidth = "210mm";
doc.documentPreferences.pageHeight = "297mm";
var page = doc.pages.add();
var margin = 20;
var tf = page.textFrames.add();
tf.geometricBounds = [margin, margin, 297 - margin, 210 - margin];
var vars = doc.textVariables;
var insertPos = tf.insertionPoints[0];
for (var i = 0; i < vars.length; i++) {
// Insert line break before each variable (dummy workaround)
if (i > 0) {
insertPos.contents = "\r";
insertPos = tf.insertionPoints.lastItem();
}
// Initialize variable with dummy instance (workaround for InDesign bug)
var dummy = tf.insertionPoints
.lastItem()
.textVariableInstances.add({ textVariable: vars[i] });
dummy.remove();
// Insert variable name as label
insertPos.contents = vars[i].name + ": ";
insertPos = tf.insertionPoints.lastItem();
// Insert variable instance
var instance = insertPos.textVariableInstances.add({ textVariable: vars[i] });
$.writeln(
"Inserted: " +
vars[i].name +
" (Index: " +
vars[i].index +
") | Instance.associatedTextVariable: " +
(instance.associatedTextVariable
? instance.associatedTextVariable.name
: "unknown"),
);
insertPos = tf.insertionPoints.lastItem();
}
$.writeln("All variables have been inserted on a new page.");