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;
}
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
...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
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
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';
}
Copy link to clipboard
Copied
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