Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Dec 23, 2008 Dec 23, 2008
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
TOPICS
Scripting
3.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 23, 2008 Dec 23, 2008
The parent of the sourceText is the story.

You want sourceText.paragraphs[0]

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 25, 2008 Dec 25, 2008
Well, that is a good way to have the paragraph which contains the index marker.

My problem is, that I want to find the index of this paragraph in the story. I mean, is it the 1st, 2nd, 3rd, or 4th (etc) paragraph in the story.

The reason I need to find this index is that I want to use it in the option of setting a topic's page references page range according to a specific number of paragraphs. For example:
INDEX
Cat, 18
Dogs, 20-25 [from a paragraph on page 20, to a paragraph on page 25]

How can I find the number of my paragraph?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 25, 2008 Dec 25, 2008
if you need to get page - you don't need paragraph index - you can get page by getting parent of ParentTextFrame of first character of your paragraph

in VB it will be something like this:

PageName = myPara.Characters.Item(1).ParentTextFrames.Item(1).Parent.Name

robin

--
www.adobescripts.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 25, 2008 Dec 25, 2008
You are right. This is also an option. But for my specific script I need it to be according to paragraphs because the page numbers might change, you see, and with paragraphs it is very accurate. I am sure there is some reasonable way to achieve this, but I failed to find it so far... Any clues?



2008/12/26, Robert_Tkaczyk < member@adobeforums.com>:
A new message was posted by Robert_Tkaczyk in


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

if you need to get page - you don't need paragraph index - you can get page by getting parent of ParentTextFrame of first character of your paragraph


in VB it will be something like this:

PageName = myPara.Characters.Item(1).ParentTextFrames.Item(1).Parent.Name

robin

--
www.adobescripts.com



------------------------------------------------------
View/reply at < http://www.adobeforums.com/webx?13@@.59b75803/2>

Replies by email are OK.
Use the unsubscribe form at < http://www.adobeforums.com/webx?280@@.59b75803!folder=.eea52bc> to cancel your email subscription.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 25, 2008 Dec 25, 2008
I don't get it. The paragraphs are just as likely to change if you're contemplating later editing.

Anyway, the way to get the number of paragraphs is:

myPara = app.activeDocument.indexes.item(0).topics.item(0).pageReferences.item(0).sourceText.paragraphs[0]
myPcount = myPara.parent.texts.itemByRange(0, myPara.index).paragraphs.length;

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 26, 2008 Dec 26, 2008
Hi,

it seems your lines should have solved the issue, but I usually get an error saying "object is invalid" on the second line. It works only if I remove the end:
myPara = app.activeDocument.indexes.item(0).topics.item(0).pageReferences.item(0).sourceText.paragraphs[0]
myPcount = myPara.parent.texts.itemByRange(0, myPara.index)//.paragraphs.length;

Did you try to run these lines and it worked fine?

You know, I think I write the bigger picture:

I am getting a Word file to typeset with index markers inside. The only problem is that Word recognizes PAGE RANGES, while InDesign does not (I mean the ones coming from Word). So my text editor and I agreed on specail "signs" to show where a page range begings and ends.
For example:
INDEX
DogsYYY, 15
DogsZZZ, 20

When I see this I know i need to change the index marker in InDesign to be like that:
INDEX
Dogs, 15-20

Now, when you do it on 1, 2, 3 index markers, it is not so bad. But if you do it on hundreds of them - the fun stops 😉 pretty crazy, ha?

So the goal is to make a script to do this simple mechanical action. And this is the bigger picture. I didn't find anything on the internet for it, which is quite surprising... Doesn't anyone else get their text from Word with index markers and page ranges?

Anyway, any sort of help will be greatly appriciated, as I am a *bit* closer to desperate.

Ola
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 26, 2008 Dec 26, 2008
I didn't actually try it and I'm in an Apple Store right now. I think I left out texts[0]. Try this:

myPcount = myPara.parent.texts.itemByRange(0, myPara.index).texts[0].paragraphs.length;

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 26, 2008 Dec 26, 2008
Hi, thanks for the quick reply!

I tryed the revised version and it worked by itslef, so I guess that really was the missing link.

Now, when I combined these 2 lines inside the bigger version, it failed to work. This is the script (Do you have any idea what's the bug?):

//InDesign CS3 Javascript
//Not working properly. Supposed to combine 2 different index markers into a single index marker containing 1 page range
//according to the original index markers location
//The real script has loops and counters, here I simplified it to see what exactely went wrong:

myIndex = app.activeDocument.indexes.item(0);
myTopics = myIndex.topics;
myTopic1 = myTopics.item(0);
myPara1 = myTopics.item(0).pageReferences.item(0).sourceText.paragraphs.item(0);
//The location of the first index marker:
myPara1Place = myPara1.parent.texts.itemByRange(0, myPara1.index).texts.item(0).paragraphs.length;
myTopic2 = myTopics.item(1);
myPara2 = myTopics.item(1).pageReferences.item(0).sourceText.paragraphs.item(0);
//The location of the second index marker:
myPara2Place = myPara1.parent.texts.itemByRange(0, myPara1.index).texts.item(0).paragraphs.length;
//sets the page reference to refer to a page range based on paragraphs:
myTopic1.pageReferences.item(0).pageReferenceType = PageReferenceType.forNextNParagraphs;
myLimit = myPara2Place - myPara1Place;
myTopic1.pageReferences.item(0).pageReferenceLimit = myLimit;
//Removes the second and uneeded topic
myTopic2.pageReferences.item(0).remove();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 26, 2008 Dec 26, 2008
Typo. You got the same paragraph count twice.

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 26, 2008 Dec 26, 2008
Thanks, I fixed it now. [myPara2Place = myPara1.parent.texts.itemByRange(0, myPara2.index).texts.item(0).paragraphs.length;]

But actually the script failed much eariler. I get the message "object is invalid" on the 4th line:
myPara1 = myTopics.item(0).pageReferences.item(0).sourceText.paragraphs.item(0);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 26, 2008 Dec 26, 2008
Well, it is possible for a topic to exist that lacks page references. Perhaps that's the problem.

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 28, 2008 Dec 28, 2008
Yup, that was the problem indeed.

Then it got stuck on the following line:
myPara1Place = myPara1.parent.texts.itemByRange(0, myPara1.index).texts.item(0).paragraphs.length;

using the Debuging tools I could see it probabely has something to do with the "itemByRange" result. What could be the bug?...

Ola
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 28, 2008 Dec 28, 2008
If you have an index item at the very end of a story, there is no paragraph there. That could be it. (I've been away from my computer all day because my daughter is in hospital having lost her baby after carrying him for eight months, so I'm more than a little distracted.)

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 29, 2008 Dec 29, 2008
In my case it isn't the last paragraph, so I guess the problem is something with my syntex.

understood. We wish you all health.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2009 Jan 03, 2009
myPara.index is the index of myPara's first insertion point, so ...texts.itemByRange (0, myPara.index) tries to range over far more Texts than there are in the story. Instead, range over characters:
>myPcount = myPara.parentStory.characters.itemByRange(0, myPara.index).paragraphs.length;

Peter
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 04, 2009 Jan 04, 2009
I didn't quite get it, how is it that it tries to range over far more texts than in the story? How is it different with the characters?

 

Ola

 

2009/1/3, Peter Kahrel < member@adobeforums.com>:
A new message was posted by Peter Kahrel in

InDesign Scripting --

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

myPara.index is the index of myPara's first insertion point, so ...texts.itemByRange (0, myPara.index) tries to range over far more Texts than there are in the story. Instead, range over characters:
myPcount = myPara.parentStory.characters.itemByRange(0, myPara.index).paragraphs.length;


Peter




View/reply at CS3 Javascript - finding the paragraph which contain the index page reference

Replies by email are OK.
Use the unsubscribe form to cancel your email subscription.



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 04, 2009 Jan 04, 2009
>myPara1Place = myPara1.parent.texts.itemByRange(0, myPara1.index)

myPara1.index returns the index of the first insertion point of the current paragraph within the story (myPara1.parent is a story), which could be in the thousands. But you're addressing probably just one text. It's as if you're asking for twenty lollies when there's just one in the jar.

When you use someobject.itemByRange (start, stop), "stop" must be smaller than (or equal to) the length of "someobject". Since there are (probably) as many characters in a story as there are indexes, in this case that's what you use for itemByRange's scope.

Peter
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 05, 2009 Jan 05, 2009
Thank you, I'll check that one and post an update...


2009/1/4, Peter Kahrel < member@adobeforums.com>:
A new message was posted by Peter Kahrel in


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


myPara1Place = myPara1.parent.texts.itemByRange(0, myPara1.index)


myPara1.index returns the index of the first insertion point of the current paragraph within the story (myPara1.parent is a story), which could be in the thousands. But you're addressing probably just one text. It's as if you're asking for twenty lollies when there's just one in the jar.


When you use someobject.itemByRange (start, stop), "stop" must be smaller than (or equal to) the length of "someobject". Since there are (probably) as many characters in a story as there are indexes, in this case that's what you use for itemByRange's scope.



Peter




View/reply at CS3 Javascript - finding the paragraph which contain the index page reference

Replies by email are OK.
Use the unsubscribe form to cancel your email subscription.



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 05, 2009 Jan 05, 2009
Ola,

Could you suppress the quoting of messages in your replies here, please. It's not really necessary to quote whole messages because the message is right here on the forum.

Small quotes to highlight a particular point are, of course, welcome.

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 27, 2009 Jan 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)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 27, 2009 Jan 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)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2009 Jan 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 30, 2009 Jan 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 31, 2009 Jan 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines