Copy link to clipboard
Copied
I am trying to get page numbers from TOC for hyperlink creation purpose, I got all page numbers other than pages mentioned in Roman letters, how to get them?
var doc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = "\\d+";
app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("TOC1");
var foundItems = doc.findGrep(true);
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
if (foundItems.length == 0) ErrorExit("No page numbers were found in the current document.", true);
for (var f = 0; f < foundItems.length; f++) {
var pageNum = foundItems[f]; // foundItems[f] returns the page number .
$.writeln(pageNum.contents);
}
The problem seems the be your grep expression, you are searching for numbers i.e. 0-9 digits and that is the reason the roman numerals are missed. Change the grep expression to make it work. Share a sample doc if you need help to write the grep expression
-Manan
Copy link to clipboard
Copied
The problem seems the be your grep expression, you are searching for numbers i.e. 0-9 digits and that is the reason the roman numerals are missed. Change the grep expression to make it work. Share a sample doc if you need help to write the grep expression
-Manan
Copy link to clipboard
Copied
Thank You Manan, found the grep and its working!
var doc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = "\\b[ivxclm]+\\b";
app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("TOC1");
var foundItems = doc.findGrep(true);
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
if (foundItems.length == 0) ErrorExit("No page numbers were found in the current document.", true);
for (var f = 0; f < foundItems.length; f++) {
var pageNum = foundItems[f]; // foundItems[f] returns the page number .
$.writeln(pageNum.contents);
}
Copy link to clipboard
Copied
I have an idea for that you might wanna try.
Instead of finding page number using find method for TOC. If you do some manipulation with TOC style for example just assign a specific character style to page number in TOC. Then generate TOC.
So while generating TOC character style will be applied automatically. So you can find page number of TOC very easily without bothering if it is real number or roman number or whatever.
Best
Sunil
Copy link to clipboard
Copied
If you are not allowed to do changes in TOC style then you need to work on Grep only or may be some different logic.
Best
Sunil
Copy link to clipboard
Copied
Never thought about this before, thank you Sunil for your nice Idea!
Copy link to clipboard
Copied
will try it