Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

Explorer ,
Nov 01, 2021 Nov 01, 2021

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

romanLetterSsF9J.png 

TOPICS
How to, Scripting

Views

83

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 1 Correct answer

Adobe Community Professional , 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

Likes

Translate

Translate
Adobe Community Professional ,
Nov 01, 2021 Nov 01, 2021

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

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

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

 

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
Adobe Community Professional ,
Nov 02, 2021 Nov 02, 2021

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

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
Adobe Community Professional ,
Nov 02, 2021 Nov 02, 2021

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

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

LATEST

will try it

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