Skip to main content
Inspiring
February 21, 2025
Answered

I want to put a character style highlight on a text variable

  • February 21, 2025
  • 2 replies
  • 431 views

Hi

I wonder if anyone can help, I'm trying to write a script where I can insert a text varible and also give it a character style highlight so it can be easily seen.

This is what I've been trying which does seem to work but I can't believe this is the way to do it

var itemVariable = app.selection[0].textVariableInstances.add({associatedTextVariable:selectedVariable});
var myTextFrame = app.selection[0].parentTextFrames[0];
var styleStartIndex = app.selection[0].index - 1;
var styleEndIndex = styleStartIndex + 1;
app.selection[0].characters.itemByRange(styleStartIndex, styleEndIndex).appliedCharacterStyle = doc.characterStyles.itemByName("Pink");

Thanks in advance

Alex

Correct answer Alex25077296wbvx

Hi again,

Thank you for your support on this, I have borrowed from the knowledge of Uwe Laubender on this and adapted your script with the following

// Apply the character style to the text variable instance
if (charStyle.isValid) {
var parent = textVarInstance.storyOffset.parent;
var index = textVarInstance.storyOffset.index;
var variableSpecialChar = parent.characters[ index ];
variableSpecialChar.appliedCharacterStyle = charStyle;
}

 This has worked nicely, I thought you would like to know. Thank you for taking the time to look into this

All the best

Alex

2 replies

Barb Binder
Community Expert
Community Expert
February 21, 2025

Hi @Alex25077296wbvx:

 

I think you have your scripting answer, so just here to add that you can also handle this with a GREP style.

 

~Barb

 

~Barb at Rocky Mountain Training
Community Expert
February 22, 2025

Super! That's a great one - forgot about this! 

 

SuperBarb!

 

Community Expert
February 21, 2025

Tested this and it worked for me

var doc = app.activeDocument;
var sel = app.selection[0];

if (sel && sel.hasOwnProperty("insertionPoints")) {
    var selectedVariable = doc.textVariables.itemByName("Test"); // Ensure this is the correct variable name

    if (selectedVariable.isValid) {
        var textVarInstance = sel.textVariableInstances.add({ associatedTextVariable: selectedVariable });

        // Find character style safely
        var charStyle;
        try {
            charStyle = doc.characterStyles.itemByName("Pink");
            if (!charStyle.isValid) throw "Character style not found!";
        } catch (e) {
            alert("Character style 'Pink' not found.");
            charStyle = doc.characterStyles[1]; // Fallback to first character style
        }

        // Apply the character style to the text variable instance
        if (charStyle.isValid) {
            textVarInstance.parent.appliedCharacterStyle = charStyle;
        }
    } else {
        alert("Text variable 'Test' not found.");
    }
} else {
    alert("Please select a valid text insertion point.");
}
Inspiring
February 23, 2025

Hi, thank you for your help on this, but isn't the parent of 'textVarInstance' the 'story' so the character style is applied to the whole story and I only want it to apply only to a text variable that is inserted into a story. I find that the textVarInstance.storyOffset gives the insertionPoints but this doesn't apply the character style to the text variable. 

Thank you again for taking the trouble with this

Alex