Text Objects And Iteration - Adobe 65009333 - InCopy CS4 - PC Manual

Scripting guide: javascript
Hide thumbs Also See for 65009333 - InCopy CS4 - PC:
Table of Contents

Advertisement

Text and Type
//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):
Text objects 37

Advertisement

Table of Contents
loading

This manual is also suitable for:

Incopy cs4

Table of Contents