Skip to main content
Known Participant
December 23, 2008
Question

CS3 Javascript - finding the paragraph which contain the index page reference

  • December 23, 2008
  • 28 replies
  • 3952 views
Hi,
I've been trying to cook a script which has the following function:
It gets a topic's page reference, and return the index in the story of the paragraph which contains the source text. I figured the line should look something like that (i made it specific for the example):

myParagraphIndex = app.activeDocument.indexes.item(0).topics.item(0).pageReferences.item(0).sourceText.parent.index;

The problem is, that this line return the index of the parent story.

How can I get the index of the containing paragraph??

Thanks,
Ola
This topic has been closed for replies.

28 replies

Participant
February 1, 2009
Yes, it work's very well with the backslashes, thank you very much for your very efficient help.
Br Scubilion.
Peter Kahrel
Community Expert
Community Expert
February 1, 2009
Br Scubilion,

It is true that JavaScript doesn't know about lookbehind, but as you're using InDesign's GREP, not JavaScript's, that's not the problem: InDesign does know lookbehind. The problem in your code is probably that you forgot to escape the backslashes. Please try the following:

"(?<=~b)\\u{1,2} \\d{1,2}"

I just assume that this should be done in VB: I know it should in JS, but as I don't know much about VB I'm guessing here.

Peter
Inspiring
February 1, 2009
Sorry, I'm not sufficiently familiar with VBScript to help you on that one. I have seen it mentioned here before (now that you bring it up). A search of this forum might help, or a VB-knowledgeable person might drop by.

Dave
Participant
February 1, 2009
Dear Sir,

I realize that the response to my snd question was in your first response, excuze me, I long to understand. In javascript, your code works very well. But in vbscript, there is a problem :
myStory.characters.itemByRange(0,myLine.index) don't give a text but a collection of characters, and I can't ask for ".texts(0).lines.length", it give me an error.

if I select the collection, like thereafter, I get an error because myselection isn't a text object !

myselection = myStory.characters.itemByRange(0,myLine.index).select
myselection.texts(0).lines.length.

And I can't use javascript, because my Grep search text "(?<=~b)\u{1,2} \d{1,2}" contains a lookbehind option that javascript doesn't understand !
Do you have another solution ?
Br. Scubilion
Inspiring
January 31, 2009
The index property of a text object is the index within the story. So, if you want the offset of a text object within some other entity, you have to do it by comparing the values of the indexes of the two entities (although, in the case of a text frame, you want the index of the first character of the frame, not the frame itself).

Dave
Participant
January 31, 2009
Thank you, Mr Dave Saunders, for your prompt response.
Just another question :
How could I get the index of the line of a grep search result, in his parent story, textframe, paragraphe, or text.
thanks
Br Scubilion
Inspiring
January 30, 2009
What you're getting is correct. All text objects return as their index property the index of the first insertion point within the text object relative to the start of the story. If you want the count of lines in the story up to and including the first line of result(1) use (JS):
myStory = result(1).parent;

myLine = result(1).lines[0];
lineCount = myStory.characters.itemByRange(0,myLine.index).texts[0].lines.length;
Dave
Participant
January 30, 2009
Hello,
I'm French user of Indesign CS3. Please, be forgiving for my poor English.
'b I've a problem when I try to get the index of a line in a Story.
Dim myindesign As InDesign.Application

Dim maselection As InDesign.Story
Dim mydocument As InDesign.Document
Set myindesign = CreateObject("InDesign.Application.CS3")
Set mydocument = myindesign.ActiveDocument
Set maselection = myindesign.Selection(1).ParentStory
myindesign.FindGrepPreferences = idNothingEnum.idNothing
myindesign.ChangeGrepPreferences = idNothingEnum.idNothing
myindesign.FindGrepPreferences.FindWhat = "(?<=~b)\u{1,2} \d{1,2}"
Set result = maselection.FindGrep(False)

If I try to get the line index of the first result like that :
myline = result(1).lines(1).index
myline is the index of the insertion point of the first characters of the result !
I can understand Vbscript and javascript code.
Thank you for your help.
frère Scubilion
Known Participant
January 27, 2009
If you want get the paragraph n-th

//1st:
story_ix_para_obj = {};
story_para_ix_obj = {};

//2nd:
story = myText.parentStory;
id = story.id;
if(!story_para_obj[id]){
story_para_obj[id] = {};
paras = story.paragraphs;
paras_len = paras.length;
for(i = 0; i< lim; i++){
story_para_obj[id] = paras;
story_para_ix_obj[paras.toSource()] = i;
}
}

//third:
this_para_start = myText.paragraphs[0];
this_para_end = myText.paragraphs[-1];

//fourth:
this_page_start = -1, this_page_end = -1;
if(this_para_start == this_para_end){
//do some thing, get para_ix/get ix_para
}else{
//do some thing, get para_ix/get ix_para
}

//fifth: combine 1-4 into index's function

Jimmy Shen

(jxswm@126.com)
Known Participant
January 27, 2009
pageRefs = myTopic.pageReferences;
pageRefsLen = pageRefs.length;
var myText1, myText2
if(pageRefsLen > 0){
myText1 = pageRefs[0].sourceText.texts[0];
if(pageRefsLen > 1){
myText2 = pageRefs[pageRefsLen-1].sourceText.texts[0];
}
}

myText1==> do some thing
myText2==> do some thing

Jimmy shen
(jxswm@126.com)