Adobe 65009333 Scripting Guide - Page 43

Changing text color, Creating and applying styles, between the formatted text and the style

Page 43 highlights

Text and Type Formatting text 43 Changing text color You can apply colors to the fill and stroke of text characters, as shown in the following script fragment (from the TextColors tutorial script): //Given two colors "myColorA" and "myColorB"... var myStory = myDocument.stories.item(0); //Enter text in the text frame. myStory.contents = "Text\rColor" var myText = myStory.paragraphs.item(0) myText.pointSize = 72; myText.justification = Justification.centerAlign; //Apply a color to the fill of the text. myText.fillColor = myColorA; //Use the itemByRange method to apply the color to the stroke of the text. myText.strokeColor = myColorB; var myText = myStory.paragraphs.item(1) myText.strokeWeight = 3; myText.pointSize = 144; myText.justification = Justification.centerAlign; myText.fillColor = myColorB; myText.strokeColor = myColorA; myText.strokeWeight = 3; Creating and applying styles While you can use scripting to apply local formatting-as in some of the examples earlier in this chapter- you probably will want to use character and paragraph styles to format your text. Using styles creates a link between the formatted text and the style, which makes it easier to redefine the style, collect the text formatted with a given style, or find and/or change the text. Paragraph and character styles are key to text-formatting productivity and should be a central part of any script that applies text formatting. The following script fragment shows how to create and apply paragraph and character styles (for the complete script, see CreateStyles): //Shows how to create and apply a paragraph style and a character style. var myParagraphStyle, myCharacterStyle, myColor, myName; //Create an example document. var myDocument = app.documents.add(); //Create a color for use by one of the paragraph styles we'll create. try{ myColor = myDocument.colors.item("Red"); //If the color does not exist, trying to get its name generates an error. myName = myColor.name; } catch (myError){ //The color style did not exist, so create it. myColor = myDocument.colors.add({name:"Red", model:ColorModel.process, colorValue:[0, 100, 100, 0]}); } var myStory = myDocument.stories.item(0); //Fill the text frame with placeholder text. myStory.textContainers[0].contents = "Normal text. Text with a character style applied to it. More normal text."; //Create a character style named "myCharacterStyle" if //no style by that name already exists.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

Text and Type
Formatting text
43
Changing text color
You can apply colors to the fill and stroke of text characters, as shown in the following script fragment
(from the TextColors tutorial script):
//Given two colors "myColorA" and "myColorB"...
var myStory = myDocument.stories.item(0);
//Enter text in the text frame.
myStory.contents = "Text\rColor"
var myText = myStory.paragraphs.item(0)
myText.pointSize = 72;
myText.justification = Justification.centerAlign;
//Apply a color to the fill of the text.
myText.fillColor = myColorA;
//Use the itemByRange method to apply the color to the stroke of the text.
myText.strokeColor = myColorB;
var myText = myStory.paragraphs.item(1)
myText.strokeWeight = 3;
myText.pointSize = 144;
myText.justification = Justification.centerAlign;
myText.fillColor = myColorB;
myText.strokeColor = myColorA;
myText.strokeWeight = 3;
Creating and applying styles
While you can use scripting to apply local formatting—as in some of the examples earlier in this chapter—
you probably will want to use character and paragraph styles to format your text. Using styles creates a link
between the formatted text and the style, which makes it easier to redefine the style, collect the text
formatted with a given style, or find and/or change the text. Paragraph and character styles are key to
text-formatting productivity and should be a central part of any script that applies text formatting.
The following script fragment shows how to create and apply paragraph and character styles (for the
complete script, see CreateStyles):
//Shows how to create and apply a paragraph style and a character style.
var myParagraphStyle, myCharacterStyle, myColor, myName;
//Create an example document.
var myDocument = app.documents.add();
//Create a color for use by one of the paragraph styles we'll create.
try{
myColor = myDocument.colors.item("Red");
//If the color does not exist, trying to get its name generates an error.
myName = myColor.name;
}
catch (myError){
//The color style did not exist, so create it.
myColor = myDocument.colors.add({name:"Red", model:ColorModel.process,
colorValue:[0, 100, 100, 0]});
}
var myStory = myDocument.stories.item(0);
//Fill the text frame with placeholder text.
myStory.textContainers[0].contents = "Normal text. Text with a character style applied
to it. More normal text.";
//Create a character style named "myCharacterStyle" if
//no style by that name already exists.