I have keywords or keyphrases that each occur one or more times in a multi-page document. For each keyphrase, how do I find the page number of each page that it occurs it?
Question posted in Javascript
A very good W3school tutorial can be found here.
A very good W3school tutorial can be found here.
2
Answers
This script takes the currently selected text, uses it as a search phrase, loops through all pages, collects the page numbers of those that contain the search phrase, formats the resulting array as
"1, 2, 3, 4"
, prints it on an alert dialog, and copies it into the clipboard, so the user can paste the resulting string where he wants.Note, I found InDesign's scripting engine to be incompatible with any non-ancient Javascript feature, hence the C-style for loops. InDesign's scripting environment also has something called
findText
andfindGrep
, which may have been useful, but I failed to find their documentation and I failed to get them to do what I wanted. Your mileage might vary.Yepp, it can be done much easier with the
findText()
method:As for the documentation, you can see a lot of examples of Extendscript scripts on Stackoverflow. Good description of the InDesign API is here:
https://www.indesignjs.de/extendscriptAPI/indesign-latest