Adobe 65009333 Scripting Guide - Page 38

Formatting text, Setting text defaults

Page 38 highlights

Text and Type Formatting text 38 //Shows the correct way to iterate through text. var myDocument = app.documents.add(); var myString = "Paragraph 1.\rDelete this paragraph.\rParagraph 2.\rParagraph 3.\rParagraph 4.\rParagraph 5.\rDelete this paragraph.\rParagraph 6.\r"; var myStory = myDocument.stories.item(0); myStory.contents = myString; //The following for loop will format all of the paragraphs by iterating //backwards through the paragraphs in the story. for(var myParagraphCounter = myStory.paragraphs.length-1; myParagraphCounter >= 0; myParagraphCounter --){ if(myStory.paragraphs.item(myParagraphCounter).words.item(0).contents == "Delete"){ myStory.paragraphs.item(myParagraphCounter).remove(); } else{ myStory.paragraphs.item(myParagraphCounter).pointSize = 24; } } Formatting text In the previous sections of this chapter, we added text to a document and worked with stories and text objects. In this section, we apply formatting to text. All the typesetting capabilities of InCopy are available to scripting. Setting text defaults You can set text defaults for both the application and each document. Text defaults for the application determine the text defaults in all new documents. Text defaults for a document set the formatting of all new text objects in that document. (For the complete script, see TextDefaults.) //Sets the text defaults of the application, which set the default formatting //for all new documents. Existing text frames are unaffected. //Set the measurement units to points. app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points; app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; //To set the text formatting defaults for a document, replace "app" //in the following lines with a reference to a document. with(app.textDefaults){ alignToBaseline = true; //Because the font might not be available, it's usually best //to apply the font within a try...catch structure. Fill in the //name of a font on your system. try{ appliedFont = app.fonts.item("Minion Pro"); } catch(e){} //Because the font style might not be available, it's usually best //to apply the font style within a try...catch structure. try{ fontStyle = "Regular"; } catch(e){} //Because the language might not be available, it's usually best //to apply the language within a try...catch structure.

  • 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
38
//Shows the correct way to iterate through text.
var myDocument = app.documents.add();
var myString = "Paragraph 1.\rDelete this paragraph.\rParagraph 2.\rParagraph
3.\rParagraph 4.\rParagraph 5.\rDelete this paragraph.\rParagraph 6.\r";
var myStory = myDocument.stories.item(0);
myStory.contents = myString;
//The following for loop will format all of the paragraphs by iterating
//backwards through the paragraphs in the story.
for(var myParagraphCounter = myStory.paragraphs.length-1; myParagraphCounter >= 0;
myParagraphCounter --){
if(myStory.paragraphs.item(myParagraphCounter).words.item(0).contents ==
"Delete"){
myStory.paragraphs.item(myParagraphCounter).remove();
}
else{
myStory.paragraphs.item(myParagraphCounter).pointSize = 24;
}
}
Formatting text
In the previous sections of this chapter, we added text to a document and worked with stories and text
objects. In this section, we apply formatting to text. All the typesetting capabilities of InCopy are available
to scripting.
Setting text defaults
You can set text defaults for both the application and each document. Text defaults for the application
determine the text defaults in all new documents. Text defaults for a document set the formatting of all
new text objects in that document. (For the complete script, see TextDefaults.)
//Sets the text defaults of the application, which set the default formatting
//for all new documents. Existing text frames are unaffected.
//Set the measurement units to points.
app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
//To set the text formatting defaults for a document, replace "app"
//in the following lines with a reference to a document.
with(app.textDefaults){
alignToBaseline = true;
//Because the font might not be available, it's usually best
//to apply the font within a try...catch structure. Fill in the
//name of a font on your system.
try{
appliedFont = app.fonts.item("Minion Pro");
}
catch(e){}
//Because the font style might not be available, it's usually best
//to apply the font style within a try...catch structure.
try{
fontStyle = "Regular";
}
catch(e){}
//Because the language might not be available, it's usually best
//to apply the language within a try...catch structure.