Skip to content
Advertisement

InDesign script to change the style of the first letter of a line

I am trying to write a script for InDesign to find the first character of every line of every paragraph, and change it to another color if it is a vowel. Since it is my very first effort in InDesign scripting, I downloaded Adobe’s Scripting Guide and managed so far to do the following:

JavaScript

At first I create the character style (red underline) and then I loop through the lines. The loop works, and finds the first character. The problem is that the style is never applied. Any help will be appreciated. Thanks!

Advertisement

Answer

As a quick fix you can replace the line:

JavaScript

with:

JavaScript

The problem is the theFirstChar in your code is just a string, a text. It has no the property appliedCharacterStyle. You have to get the object character from the story/paragraph/line: stories[0].paragraphs[counter].lines[counter].character[0] if you want to apply a character style on it.

Note: the paragraphs.item(theParaCounter) is the same as paragraphs[theParaCounter], lines.item(theLineCounter) is the same as lines[theLineCounter].


Additionally the condition can be shortened:

JavaScript

instead of:

JavaScript

.toLowerCase() makes the condition case insensitive. If you need it.


The main() function can be boiled down to this:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement