Copy link to clipboard
Copied
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;
1 Correct answer
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
...
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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';
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.

