Text objects and iteration. Adobe InDesign CS6
Anúncio
Anúncio
C HAPTER 5: Text and Type Working with Text Frames 85
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. (We omitted the myGetBounds
function from this listing; you can find it in
“Creating a text frame” on page 70
,” or see the
TextIterationWrong tutorial script.)
--The following for loop will fail to format all of the paragraphs
--and then generate an error.
repeat with myParagraphCounter from 1 to (count paragraphs of myStory) if contents of word 1 of paragraph myParagraphCounter of myStory is "Delete" then tell paragraph myParagraphCounter of myStory to delete else set point size of paragraph myParagraphCounter of myStory to 24 end if end repeat
In the preceding example, some of the 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 that begin with the word “Delete.” When the script deletes the second paragraph, the third paragraph moves up to take its place. When the loop counter reaches 3, 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. (We omitted the myGetBounds
function from this listing; you can find it in
“Creating a text frame” on page 70
,” or see the TextIterationRight tutorial script.)
--By iterating backwards we can avoid the error.
repeat with myParagraphCounter from (count paragraphs of myStory) to 1 by -1 if contents of word 1 of paragraph myParagraphCounter of myStory is "Delete" then tell paragraph myParagraphCounter of myStory to delete else set point size of paragraph myParagraphCounter of myStory to 24 end if end repeat
Working with Text Frames
In the previous sections of this chapter, we concentrated on working with text stream objects; in this section, we focus on text frames, the page-layout items that contain text in an InDesign document.
Linking text frames
The nextTextFrame
and previousTextFrame
properties of a text frame are the keys to linking (or
“threading”) text frames in InDesign scripting. These properties correspond to the in port and out port on
InDesign text frames, as shown in the following script fragment (for the complete script, see
LinkTextFrames):
Download
Anúncio