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

How to get page Numbers mentioned in Roman letters from TOC using Javascript

Engaged ,
Nov 01, 2021 Nov 01, 2021

 

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);
}

romanLetterSsF9J.png 

TOPICS
How to , Scripting
917
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

correct answers 1 Correct answer

Community Expert , Nov 01, 2021 Nov 01, 2021

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

Translate
Community Expert ,
Nov 01, 2021 Nov 01, 2021

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

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
Engaged ,
Nov 02, 2021 Nov 02, 2021

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);
}

 

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
Advocate ,
Nov 02, 2021 Nov 02, 2021

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

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
Advocate ,
Nov 02, 2021 Nov 02, 2021

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

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
Engaged ,
Nov 02, 2021 Nov 02, 2021

Never thought about this before, thank you Sunil for your nice Idea!

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
Engaged ,
Nov 02, 2021 Nov 02, 2021
LATEST

will try it

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