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

Get Page number of the search text

Participant ,
Oct 26, 2022 Oct 26, 2022

Copy link to clipboard

Copied

Hi,

I tried to fetch the count of a particular text in a document according to pages but it does nothing, for that, I used

var myDocument = app.activeDocument;
var myPages = myDocument.pages.length;

app.findGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = "Hi";
var list = app.activeDocument.findGrep ();
for(var i =0; i<= list.length; i++)
{
alert("No of Occurence in page "+myPages +" is "+ list[myPages].length);
app.findGrepPreferences = NothingEnum.NOTHING;

}

-Monisha
TOPICS
Scripting

Views

57

Likes

Translate

Translate

Report

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

correct answers 2 Correct answers

Community Expert , Oct 26, 2022 Oct 26, 2022

Review your code please. So list would be an array of text/word objects now in this collection you are a picking an element at myPages which is the number of pages in the document which is fixed.

So let's say you have a document with 10 pages. And you have Hi on page number 1 and 5. So myPages would be 10 and list would be an array on length 2. So list[myPages] i.e. list[10] as list has only 2 elements would be undefined and for cases where you would get a valid document the length would always

...

Likes

Translate

Translate
Participant , Oct 26, 2022 Oct 26, 2022

Hi @Manan Joshi 

 

Thanks for your feedback to identify my mistake. By modifying the above code, I am now able to retrieve the page number of the searched text. Here is the code I used.

 

var myDocument = app.activeDocument;
var myPages = myDocument.pages.length;
app.findGrepPreferences=NothingEnum.NOTHING; 
app.findGrepPreferences.findWhat = 'Hi'; 
var find = app.activeDocument.findGrep (); 
for (var i = 0; i < find.length; i++) { 
var getSearchTextPageNumber = find [i].parentTextFrames[0].parentPage.nam

...

Likes

Translate

Translate
Community Expert ,
Oct 26, 2022 Oct 26, 2022

Copy link to clipboard

Copied

Review your code please. So list would be an array of text/word objects now in this collection you are a picking an element at myPages which is the number of pages in the document which is fixed.

So let's say you have a document with 10 pages. And you have Hi on page number 1 and 5. So myPages would be 10 and list would be an array on length 2. So list[myPages] i.e. list[10] as list has only 2 elements would be undefined and for cases where you would get a valid document the length would always give the value 2 i.e. length of the "Hi"

Try to fix this before you move on to getting the page.

-Manan

Likes

Translate

Translate

Report

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 ,
Oct 26, 2022 Oct 26, 2022

Copy link to clipboard

Copied

Hi @Manan Joshi 

 

Thanks for your feedback to identify my mistake. By modifying the above code, I am now able to retrieve the page number of the searched text. Here is the code I used.

 

var myDocument = app.activeDocument;
var myPages = myDocument.pages.length;
app.findGrepPreferences=NothingEnum.NOTHING; 
app.findGrepPreferences.findWhat = 'Hi'; 
var find = app.activeDocument.findGrep (); 
for (var i = 0; i < find.length; i++) { 
var getSearchTextPageNumber = find [i].parentTextFrames[0].parentPage.name + '\n'; 

}

 

 

-Monisha

Likes

Translate

Translate

Report

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 ,
Oct 26, 2022 Oct 26, 2022

Copy link to clipboard

Copied

LATEST

Good work. Now some more pointers on your latest improved code snippet. The myPages variable is not used you can remove it. In the statement find [i].parentTextFrames[0].parentPage.name what would happen in case the textframe is on the pasteboard, i.e. does not have a valid parentPage? You might want to put in appropriate checks for this condition.

-Manan

 

Likes

Translate

Translate

Report

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