1 person found this helpful
You don't have to refer to paragraphs, you can immediately use words:
always shows the first word in your current text frame. Do note, however, that a word may be split due to hyphenation (it shows the entire word). An additional, eh, 'feature' (?) is that blank lines are ignored. A good solution, however, would be to go one step down and use characters:
Excellent, thank you. I take your point about hyphenation, and I'm working with just the first character.
Now to work through the looping; should be straightforward enough.
Okay, perhaps I can ask for a little bit more guidance?
I have this (I know, perhaps a bit verbose):
var myDocument = app.activeDocument;
for (var i = myDocument.pages.length-1; i >= 0; i--)
var myPage = myDocument.pages.item(i);
var myTextFrame = myPage.textFrames.item(0);
var myText = myTextFrame.characters.item(0);
var myWord = myText.parentTextFrames.characters;
which works nicely as long as there is a text frame on every page. As I mentioned, there will be blank versos, graphics, etc., along the way, so I want to skip those.
I've been trying if statements such as
and other variations to see where the logic should work, but I'm missing something.
Again, much thanks!
It's as straightforward as anything in InDesign. Well, up to a certain point
Every document has a Pages array so you can use .length to loop over all. The pages do not correspond to 'page numbers', as these are fleety things. The array starts at '0' for the first page and continues to the end of the doc.
Similarly, each page can immediately refer to its contents -- in this case you want just the TextFrames. Do not use PageItems, as this holds "all" items, including graphics, lines, circles and whatnot. You can loop over all of these text frames inside the pages loop.
Wanna have a go at it yourself?
But of course! I appreciate the help. I think I'm following you, and will keep at it.
Well, you were on the right track, but testing 'myText' fails if there is no text frame on a page because it fails one line earlier
>var myTextFrame = myPage.textFrames.item(0);
just before this, you should test the number of text frames:
if (myPage.textFrames.length > 0)
... your stuff ..
Only use this if you are absolutely positive there is just a single text frame on each page! A slightly better way would be to always loop over each textframe:
for (frames=0; frames<myPage.textFrames.length; frames++)
var myTextFrame = myPage.textFrames.item(frames);
in which case you also don't have to test its length first (the loop will not be executed if there are zero text frames).
[Techy] Since you have only one continuous story, an even better way would be to loop over the textframes of that story alone. You have to identify the story somehow, and I usually click the text cursor in the one I need:
myStory = app.selection.parentStory;
or you can rely on the fact that the story starts in the only text frame on page 1:
myStory = app.activeDocument.pages.stories;
(I think that oughta work.) Then, each frame of this story up till the end can be found in the array
which are usually text frames. You can loop over these using
for (frame=0; frame<myStory.textContainers.length; frame++)
myTextFrame = myStory.textContainers[frame];
Sorry for the delay in responding, and thanks very much for your help. It's working exactly as it should! I'm just trying to resolve implications--i.e., it's doing what I asked, but I'm not so sure I should have asked it to do that!