Saturday, August 25, 2007

 

Selecting Paragraphs

This is a real-time blog. I don't know the answer as I start to write this. I have a paragraph reference and a number, n. I want to select n paragraphs starting with the referenced paragraph.

The first question anyone ever asks about a problem like this is: why are you selecting text in a script? You don't need to. All you need do is reference it.

Well, I'm selecting it because this is a feature in an interactive script. But of course, in order to select it, I need to create that reference. And once I have the reference, all will be well. Perhaps the quickest solution is to walk through the paragraphs, until I reach the last one and then grab a text reference from the first character of the first paragraph to the last of the last. So, how would that go:
function getTextRef(myPara, n) {
var tf = myPara.parent.texts[0]; // text flow
var s = myPara.index; // start
var e = myPara.characters[-1].index; // end
for (j = n-1; j > 0; j--) {
myPara = myPara.insertionPoints[-1].paragraphs[0];
e = myPara.characters[-1].index; // updated end
}
return tf.characters.itemByRange(s, e).texts[0];
}
And that just about does the trick. There is one possible fly-in-the-ointment I can think of: what if there isn't enough text to serve up the n paragraphs? A simple try/catch will handle that. Here's the final version of the function inside the text script I used to test it:
myPara = app.selection[0].paragraphs[0];
n = 2;
selectIt(getTextRef(myPara, n));

function getTextRef(myPara, n) {
var tf = myPara.parent.texts[0]; // text flow
var s = myPara.index; // start
var e = myPara.characters[-1].index; // end
for (j = n-1; j > 0; j--) {
try {
myPara = myPara.insertionPoints[-1].paragraphs[0];
} catch (e) { break }
e = myPara.characters[-1].index; // updated end
}
return tf.characters.itemByRange(s, e).texts[0];
}

function selectIt(theObj) {
app.select(theObj,SelectionOptions.replaceWith);
app.activeWindow.zoom(ZoomOptions.fitPage);
app.activeWindow.zoomPercentage = 150
}
Well, that was easy. But I have to believe it would have taken longer if I hadn't chatted to myself here.

Comments:
Hi.
I have a problem with

var myDialog = app.dialogs.add({name:"FoTheText",canCancel:true});
with(myDialog){with(dialogColumns.add()){var myTextEditField = textEditboxes.add({editContents:"Tutorial",minWidth:180});}}
var myResult = myDialog.show();
if(myResult == true){var myString = myTextEditField.editContents;myDialog.destroy();



// That is the place where i can't understand script. I mean how insert text in the first textframe on the first page and before first para in the stories?
//Couse when i try to do this:

app.activeDocument.stories[0].insertionPoints.item(0).contents = myString + "\r"

//That insert text in the first frame on the masterPagespreads :D
//that's so funny, couse when i start looking for string Hello World I can't find it first time...


}


else{myDialog.destroy();}
 
If you want text in the first frame of the first page, why not just address that:

app.activeDocument.pages[0].textFrames[0]

Stories[0] might be on some other page completely.

Dave
 
Hi Dave,

I know it's an old post but I have a question related to this, I'm wondering if you could help me. I'd like to select all the text between two exact paragraph styles with a script in InDesign. Is it possible?

Thanks a lot in advance,

Zita
 
Zita,

Just go about it the way I did in this post. Establish references to the start and end (presumably by searching the story) and then select away.

Dave
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?