Adobe 65009333 Scripting Guide - Page 37

Text objects and iteration

Page 37 highlights

Text and Type Text objects 37 //Shows how to remove formatting from text as you move it //to other locations in a document. //Create an example document. var myDocument = app.documents.add(); var myStory = myDocument.stories.item(0); myStory.contents = "This is a formatted string.\rText pasted after this text will retain its formatting.\r\rText moved to the following line will take on the formatting of the insertion point.\rItalic: "; //Apply formatting to the first paragraph. myStory.paragraphs.item(0).fontStyle = "Bold"; //Apply formatting to the last paragraph. myStory.paragraphs.item(-1).fontStyle = "Italic"; //Copy from one frame to another using a simple copy. app.select(myStory.paragraphs.item(0).words.item(0)); app.copy(); app.select(myStory.paragraphs.item(1).insertionPoints.item(-1)); app.paste(); //Copy the unformatted string from the first word to the end of the story //by getting and setting the contents of text objects. Note that this does //not really copy the text; it replicates the text string from one text //location to another. myStory.insertionPoints.item(-1).contents = myStory.paragraphs.item(0).words.item(0).contents; Text objects and iteration When your script moves, deletes, or adds text while iterating through a series of text objects, you can easily end up with invalid text references. The following script demonstrates this problem (for the complete script, see TextIterationWrong): //Shows how *not* 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 fail to format all of the paragraphs. for(var myParagraphCounter = 0; myParagraphCounter < myStory.paragraphs.length; myParagraphCounter ++){ if(myStory.paragraphs.item(myParagraphCounter).words.item(0).contents == "Delete"){ myStory.paragraphs.item(myParagraphCounter).remove(); } else{ myStory.paragraphs.item(myParagraphCounter).pointSize = 24; } } In the above example, some paragraphs are left unformatted. How does this happen? The loop in the script iterates through the paragraphs from the first paragraph in the story to the last. As it does so, it deletes paragraphs beginning with "Delete." When the script deletes the second paragraph, the third paragraph moves up to take its place. When the loop counter reaches 2, the script processes the paragraph that had been the fourth paragraph in the story; the original third paragraph is now the second paragraph and is skipped. To avoid this problem, iterate backward through the text objects, as shown in the following script (from the TextIterationRight tutorial script):

  • 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
Text objects
37
//Shows how to remove formatting from text as you move it
//to other locations in a document.
//Create an example document.
var myDocument = app.documents.add();
var myStory = myDocument.stories.item(0);
myStory.contents = "This is a formatted string.\rText pasted after this text will
retain its formatting.\r\rText moved to the following line will take on the formatting
of the insertion point.\rItalic: ";
//Apply formatting to the first paragraph.
myStory.paragraphs.item(0).fontStyle = "Bold";
//Apply formatting to the last paragraph.
myStory.paragraphs.item(-1).fontStyle = "Italic";
//Copy from one frame to another using a simple copy.
app.select(myStory.paragraphs.item(0).words.item(0));
app.copy();
app.select(myStory.paragraphs.item(1).insertionPoints.item(-1));
app.paste();
//Copy the unformatted string from the first word to the end of the story
//by getting and setting the contents of text objects. Note that this does
//not really copy the text; it replicates the text string from one text
//location to another.
myStory.insertionPoints.item(-1).contents =
myStory.paragraphs.item(0).words.item(0).contents;
Text objects and iteration
When your script moves, deletes, or adds text while iterating through a series of text objects, you can
easily end up with invalid text references. The following script demonstrates this problem (for the
complete script, see TextIterationWrong):
//Shows how *not* 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 fail to format all of the paragraphs.
for(var myParagraphCounter = 0; myParagraphCounter < myStory.paragraphs.length;
myParagraphCounter ++){
if(myStory.paragraphs.item(myParagraphCounter).words.item(0).contents ==
"Delete"){
myStory.paragraphs.item(myParagraphCounter).remove();
}
else{
myStory.paragraphs.item(myParagraphCounter).pointSize = 24;
}
}
In the above example, some paragraphs are left unformatted. How does this happen? The loop in the
script iterates through the paragraphs from the first paragraph in the story to the last. As it does so, it
deletes paragraphs beginning with “Delete.” When the script deletes the second paragraph, the third
paragraph moves up to take its place. When the loop counter reaches 2, the script processes the paragraph
that had been the fourth paragraph in the story; the original third paragraph is now the second paragraph
and is skipped.
To avoid this problem, iterate backward through the text objects, as shown in the following script (from
the TextIterationRight tutorial script):