Skip to main content
Participating Frequently
January 20, 2009
Question

[CS3 JS] Add formatted text

  • January 20, 2009
  • 6 replies
  • 433 views
I'm working on a kind of templating script. This script adds text to the document depending on some input data, so the text can be almost anything.
My question is whether there is a convenient way to add formatted text to eg. a given text frame. By formatted I mean a character style applied to it.

I can think of a solution like inserting the text, then find it and then apply the character style.

Is there a simplier way, by which I mean a built in function or so?
Or has someone already written such a function?
If not then this work is waiting for me, and when ready I'll share the results :)

Thanks in advance for your help!
Balázs
This topic has been closed for replies.

6 replies

Loic.Aigon
Legend
January 22, 2009
Nice!
Participating Frequently
January 22, 2009
You can set the properties of your insertion point before inserting the text.

var ip = app.activeDocument.textFrames.item(0).insertionPoints.item(200);
ip.appliedCharacterStyle = "BigRed";
ip.contents = "Hello, world!";

You could even do it in one step:

app.activeDocument.textFrames.item(0).insertionPoints.item(100).properties = { appliedCharacterStyle : "BigRed", contents: "Hello, world!" };

Hth,
Dirk
Participating Frequently
January 20, 2009
I've been around to use XML, but at first glance it seemed difficult... But now I've forced myself to read the help, and made some experiments with this mapping thing, and I think this might be the tool I need, though I have to revise the things I've already done.

Thanks for the advice :)
Loic.Aigon
Legend
January 20, 2009
AFAIK,
You can do as you suggested : place, find and apply style.
However, you may wonder about the process itself.
As you create a template, why not thinking about using xml (or tagged text maybe but don't really know nothing about that technique).
With xml, you can map a style to a tag. Hence, when you place the xml file, it will throw the text with the style applied at the first place.
What I want to say, is that, as you want to automate things, maybe you can consider richer solutions.
Loic
Participating Frequently
January 20, 2009
Sorry, I've forgotten to mention, that the text frame into which the text is inserted may contain other texts which shall retain their formatting, and the insertion point where the text is added may also vary. Furthermore the inserted text is usually not a complete paragraph, but added to a paragraph, that's why I use character styles.

However thanks for your fast reply!
Balázs
Loic.Aigon
Legend
January 20, 2009
//Given a textFrame "myTextFrame" & a character style "myCharStyle"
myTextFrame.contents = "Here is my text";
myTextFrame.texts[0].appliedCharacterStyle = app.activeDocument.characterStyles.item("myCharStyle");
Loic