Skip to main content
Participating Frequently
August 6, 2024
Answered

Apply Character Style to Content Not Word

  • August 6, 2024
  • 1 reply
  • 271 views

I am creating a list of all weights and styles of a font family by script.
The list looks like this:

Arial:  Regular | Italic | Bold | Bold Italic |

 

For every font weight/style there is a character style with the same name.

I try to apply this character style to every term in the list, but if I work with

myFrame.parentStory.lines[1].words[i].appliedCharacterStyle=myStyles[i];

it is not working, because some styles have two words (like Bold Italic) and I  also want to use the | as seperator...

Is there a better solution to achieve this? here is  the for each loop:

for ( var i = 0; i < myStyles.length; i++ ) {
     myFrame.insertionPoints[-1].contents=myStyles[i]+" ";
     myFrame.parentStory.words[i].appliedCharacterStyle=myStyles[i];
};



This topic has been closed for replies.
Correct answer Robert at ID-Tasker

You should reverse operations in your loop - first, apply CharStyle to the insertion point - then set contents:

 

 

 

for ( var i = 0; i < myStyles.length; i++ ) {
myFrame.insertionPoints[-1].appliedCharacterStyle=myStyles[i].name;
myFrame.insertionPoints[-1].contents=myStyles[i];

myFrame.insertionPoints[-1].appliedCharacterStyle='[None]';
myFrame.insertionPoints[-1].contents=myStyles[i]+" | ";
};

 

 

You might need to make a reference to the '[None]' CharStyle and use this reference instead of just text - I'm not JS guy and I'm responding from my phone. 

 

And you shouldn't be adding contents to the TextFrame - but to the ParentStory - as you can run "out of space".

 

1 reply

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
August 6, 2024

You should reverse operations in your loop - first, apply CharStyle to the insertion point - then set contents:

 

 

 

for ( var i = 0; i < myStyles.length; i++ ) {
myFrame.insertionPoints[-1].appliedCharacterStyle=myStyles[i].name;
myFrame.insertionPoints[-1].contents=myStyles[i];

myFrame.insertionPoints[-1].appliedCharacterStyle='[None]';
myFrame.insertionPoints[-1].contents=myStyles[i]+" | ";
};

 

 

You might need to make a reference to the '[None]' CharStyle and use this reference instead of just text - I'm not JS guy and I'm responding from my phone. 

 

And you shouldn't be adding contents to the TextFrame - but to the ParentStory - as you can run "out of space".

 

viktor_nAuthor
Participating Frequently
August 6, 2024

Great, thank you!! I've created an additional charater Style, 'noStyle', and it works!
Will also check ParentStory, it is my first script.. 🙂