1

I need a script for InDesign which do the following: insert text string from variable and format it with existing character style.

The previous to last string does not working in my attempt:

var myNewText = "someText";
var myStyle = app.activeDocument.characterStyles.item("Inserted Text");
myTextFrame = app.selection[0];
firstInsertionPoint = myTextFrame.insertionPoints[-1].index;
myNewText.appliedCharacterStyle = myStyle; // THIS NOT WORKING
myTextFrame.contents += myNewText;
1
  • Hey Dmitry and welcome to GraphicDesign! Let us know if you have any questions about how the site works Mar 18, 2016 at 14:57

1 Answer 1

1

The problem is that you are trying to format the string - which is not an object in InDesign and therefore cannot be formatted via InDesign styles.

Instead you need to first add the string to the contents as you did in your last line, which will transform your string into real text objects (like words, characters, etc.) which you can format then.

To figure out which parts of the text in your text frame to edit you need to find two indices, those where the formatting begins and ends, the most appropriate would be on a character level. The start index will just be your last character + 1 and the end index is this start index plus the length of the string to add (-1).

The character style does not necessarily be saved in a variable first and the firstInsertionPoint does not do anything in your script. I edited your script to fix your issue and to make a bit more clear (I hope), what's happening. It should work this way.

var myNewString = "someString";
var myTextFrame = app.selection[0];

var styleStartIndex = myTextFrame.characters.lastItem().index + 1;
var styleEndIndex = styleStartIndex + myNewString.length - 1;

myTextFrame.contents += myNewString;
myTextFrame.characters.itemByRange(styleStartIndex, styleEndIndex).appliedCharacterStyle = "Inserted Text";

Edit

Ok, since you need to insert the text at the current text cursor position, the script needs to look a little different. From your original code I assumed you need to insert it at the end of the text frame. I'll leave in my original answer with the explanation, as it might help people who have similar problems.

Here is the modified code that should do, what you actually want to do:

var myNewString = "someString";
var myTextFrame = app.selection[0].parentTextFrames[0];

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

app.selection[0].contents = myNewString;
myTextFrame.characters.itemByRange(styleStartIndex, styleEndIndex).appliedCharacterStyle = "Inserted Text";
6
  • Hey! Thank you for the answer! But I'm getting an error in your code line 4 (Error number: 45; Object is invalid): var styleStartIndex = myTextFrame.characters.lastItem().index + 1; Have you checked the script? Is it working on your machine? Mar 24, 2016 at 9:39
  • Yes, it's working on my machine. Did you make sure you selected a text frame first?
    – mdomino
    Mar 25, 2016 at 9:02
  • Sure! Checked on 2 machines with the same result (OS X 10.11.4, InDesign CC 2015.3) Mar 28, 2016 at 13:12
  • This is really strange. Are you sure there is text in your text frame? If this also needs to work on empty text frames, the snippet would need to be modified a bit. Also: By selecting I mean you have to select the actual frame with the black arrow, not have your text cursor in the frame.
    – mdomino
    Mar 29, 2016 at 20:17
  • Oh, now I realised, the script is working, but not in the way I need. The objective is to insert text in the exact place of paragraph with cursor in the frame. Sorry for this! May be you can help? Mar 30, 2016 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.