Skip to main content
Participant
October 13, 2009
Question

characterStyle won't apply to textRange

  • October 13, 2009
  • 1 reply
  • 1327 views

I have the following code:

changeLayer.textFrames.getByName(textKeys).contents = textArray[textKeys];

var legalStyle = doc.characterStyles.add("legal");

var charAtts = legalStyle.characterAttributes;

charAtts.leading=6;

charAtts.size=20;

legalStyle.applyTo(changeLayer.textFrames.getByName(textKeys).textRange);

Which is pretty much identical to the example (page 24) in the javaScripting reference for AI CS3 (what I'm using). However, the style isn't taking effect. No errors, no notifications, just no change in the text. Any idea what I'm doing wrong? Thanks.

This topic has been closed for replies.

1 reply

Patrice Brel
Inspiring
October 13, 2009

Good evening,

this script, created on the basis of the same reference (scripting javascript) seems ok.

var docRef = app.activeDocument;
var myLayer = docRef.activeLayer;
var textRef1 = myLayer.textFrames.getByName("T1"); // T1 = the name of an existing textFrame
textRef1.contents = "OK";
textRef1.top = 700;
textRef1.left = 50;
var charStyle = docRef.characterStyles.getByName("BigRed"); // an existing style
charStyle.applyTo(textRef1.textRange);

Remark: we need to save the illustrator file when we create a style with script before it appears in the styles box.

Patrice

sabrikaAuthor
Participant
October 14, 2009

Good Morning,

Did you create the character style via script, or manually? I am creating my style via script, as you can see. I've modified the script slightly, to no avail.

CODE:

changeLayer.textFrames.getByName(textKeys).contents = textArray[textKeys];

var legalStyle = doc.characterStyles.add("legal");

var charAtts = legalStyle.characterAttributes;

charAtts.leading=6;

charAtts.size=20;

var textRef1 = changeLayer.textFrames.getByName(textKeys);

legalStyle.applyTo(textRef1.textRange);

I modified it to be virtually identical to your script.

Can you elaborate on your last remark? Are you saying the style won't be applied unless the document is saved before calling the applyTo function?

Thanks.

Patrice Brel
Inspiring
October 14, 2009

Good evening,

I will try your script but before I will respond to your last answers.

I tried to redraw the illustrator file to see the new style created with your process. Redraw() was not enough. I need to save the file before the new style appears in the characters styles box.

However another problem is that if your style exists yet you will receive an error message. That's also the case even you don't see your existing style in the characters style box (situation just after using the script for the first time).

I will verify but I think my script apply directly the new style.

See you later

Patrice