Skip to main content
Inspiring
Feb 21, 2025 4:42 PM UTC
Answered

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

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

Alex25077296wbvxCorrect answer
Inspiring
Feb 23, 2025 11:55 AM UTC

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

5 replies

Barb Binder
Community Expert
Community Expert
Feb 21, 2025 7:55 PM UTC

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

 

Translate
~Barb at Rocky Mountain Training
Community Expert
Feb 22, 2025 8:46 AM UTC

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

 

SuperBarb!

 

Translate
Community Expert
Feb 21, 2025 6:20 PM UTC

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.");
}
Translate
Inspiring
Feb 23, 2025 9:37 AM UTC

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

Translate