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

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

Community Beginner ,
Jun 07, 2023 Jun 07, 2023

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;

 

TOPICS
Scripting
284
Translate
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

correct answers 1 Correct answer

Community Expert , Jun 07, 2023 Jun 07, 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.charact
...
Translate
Community Expert ,
Jun 07, 2023 Jun 07, 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;
Translate
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 ,
Jun 07, 2023 Jun 07, 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.

Translate
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 ,
Jun 07, 2023 Jun 07, 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';

 

Translate
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 ,
Jun 07, 2023 Jun 07, 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?

Translate
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 ,
Jun 07, 2023 Jun 07, 2023
LATEST

Ignore what I said about it's not working if the style group name contains a space. I just tried it again and it worked. Maybe I unknowingly fixed something.

 

Thanks again.

Translate
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