Skip to main content
Known Participant
June 7, 2023
Answered

Help with script to insert text with an applied style from a style group

  • June 7, 2023
  • 2 replies
  • 411 views

I know very little about scripting but have been able to cobble together a a few things for my own use.

 

Now I'm trying to write a script to insert text at the insertion point with an applied character style from a style group, with the text string, the style name, and the style group name all defined in the script. The following is what I've cobbled together from examples I've found.

 

If I leave out the third line that defines myStyle and change the reference in the last line to the character style name "Prime" it works -- but the character style "Prime" can't be in a style group.

 

I'd really appreciate any help with that issue or with making the script less kludgy overall. thanks.

 

var myNewString = String.fromCharCode(0x2032); // prime mark
var myTextFrame = app.selection[0].parentTextFrames[0];
var myStyle = app.documents[0].paragraphStyleGroups.item("Auto").paragraphStyles.item("Prime");

var styleStartIndex = app.selection[0].index;
var styleEndIndex = styleStartIndex + myNewString.length - 1;

app.selection[0].contents = myNewString;
myTextFrame.characters.itemByRange(styleStartIndex, styleEndIndex).appliedCharacterStyle = myStyle;

 

This topic has been closed for replies.
Correct answer brian_p_dts

You were almost there. You defined myStyle as a paragraph style instead of a character style. I've changed it here. 

var myNewString = String.fromCharCode(0x2032); // prime mark
var myTextFrame = app.selection[0].parentTextFrames[0];
var myStyle = app.documents[0].characterStyleGroups.item("Auto").characterStyles.item("Prime");

var styleStartIndex = app.selection[0].index;
var styleEndIndex = styleStartIndex + myNewString.length - 1;

app.selection[0].contents = myNewString;
myTextFrame.characters.itemByRange(styleStartIndex, styleEndIndex).appliedCharacterStyle = myStyle;

2 replies

Adobe Expert
June 7, 2023

It's much easier first to apply the character style to the insertion point, then enter the text. That way you needn't bother with the lengty of the text you enter:

 

app.selection[0].appliedCharacterStyle = myStyle;
app.selection[0].contents = myNewString;

 

Also, to set a variable to a string value you can use JavaScript's \u0000 format:

 

var myNewString = '\u2032';

 

James_M_SAuthor
Known Participant
June 7, 2023

Thanks a lot, @Peter Kahrel. I thought there had to be a less convoluted way of doing something simple like this, but I couldn't find it.

 

Another question about this: It doesn't seem to work if the style group name has a space in it. If the style group name is "Auto," as in my example, it works; but if the style group name is something like "Auto styles" it won't work. Spaces in the style name itself doesn't seem to matter. Is there something else I'm missing?

brian_p_dts
brian_p_dtsCorrect answer
Adobe Expert
June 7, 2023

You were almost there. You defined myStyle as a paragraph style instead of a character style. I've changed it here. 

var myNewString = String.fromCharCode(0x2032); // prime mark
var myTextFrame = app.selection[0].parentTextFrames[0];
var myStyle = app.documents[0].characterStyleGroups.item("Auto").characterStyles.item("Prime");

var styleStartIndex = app.selection[0].index;
var styleEndIndex = styleStartIndex + myNewString.length - 1;

app.selection[0].contents = myNewString;
myTextFrame.characters.itemByRange(styleStartIndex, styleEndIndex).appliedCharacterStyle = myStyle;
James_M_SAuthor
Known Participant
June 7, 2023

Thanks for the reply, @brian_p_dts. I should have caught that myself, but I was expecting the problem with the script to be bigger than that. But that's what I get for not looking closely enough at what I'm cutting and pasting.