Highlighted

How to get full paragraph number as displayed on the page in ExtendScript

Participant ,
Jun 29, 2020

Copy link to clipboard

Copied

I need to add page headers to a long and complex book. The book is hierarchically structured into numeric paragraphs and subparagraphs, and the page headers must reflect the first and last paragraph numbers on each page; e.g., if §5.14 starts at the top of page 109 and §5.17.2 starts at the bottom of the same page, the page header should say “§5.14 – 5.17.2”.

 

The numbered paragraphs are all in a numbered list, so the numbers are automatic.

 

Since, very annoyingly, auto-numbers like these cannot be extracted into text variables, making the headers is proving to be a bit of a headache. Manually doing it is not an option. The best way I’ve come up with (based on this StackExchange post) is to make a script that:

 

  1. Iterates through all paragraphs in the document
  2. If the paragraph has a numbered heading style applied, insert an anchored text frame (with a specific object style applied, which is non-printing and applies the paragraph style the page header text variables are based on)
  3. Populate the text frame with the number of the paragraph (note: the full number, e.g., “5.17.2” – not just “2”)

 

The two first were fairly easy, but I’m stumped for the third. I can easily retrieve the current paragraph’s individual number (“2”) or the formatting applied to the numbering (e.g., “^1.^2.^#^t”), but how to I get the numbers from any previous level? How to I get the full, formatted string “5.17.2”?

 

I suppose I could use Paragraph.convertBulletsAndNumberingToText(), then use GREP replace to delete everything after the numbering, but that seems extremely hacky for something so rather fundamental to a nested list as accessing the fully qualified number. Moreover, convertBulletsAndNumberingToText() can only be applied to an existing Paragraph object and has the bizarre and counterintuitive behaviour of returning undefined, rather than returning the converted string as you’d expect. I’m not sure I’d even be able to duplicate the paragraph into the anchored text frame and textify the numbering at all – the act of duplicating the paragraph might just increase the numbering so I’d end up with “5.17.3” as the text, rather than “5.17.2” …

 

Is there really no simple, sane function to extract this?

Okay, so it seems I was looking through the properties of the Paragraph class too narrowly – I was looking under N for numberingXXX, and it turns out there is a property called Paragraph.bulletsAndNumberingResultText which gives me exactly what I’m looking for (well, nearly – that property also includes the tab that follows the numbering marker, but that should be easy to get rid of).

 

So simple, and yet it took me hours to locate. 😕

TOPICS
Scripting

Views

75

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

How to get full paragraph number as displayed on the page in ExtendScript

Participant ,
Jun 29, 2020

Copy link to clipboard

Copied

I need to add page headers to a long and complex book. The book is hierarchically structured into numeric paragraphs and subparagraphs, and the page headers must reflect the first and last paragraph numbers on each page; e.g., if §5.14 starts at the top of page 109 and §5.17.2 starts at the bottom of the same page, the page header should say “§5.14 – 5.17.2”.

 

The numbered paragraphs are all in a numbered list, so the numbers are automatic.

 

Since, very annoyingly, auto-numbers like these cannot be extracted into text variables, making the headers is proving to be a bit of a headache. Manually doing it is not an option. The best way I’ve come up with (based on this StackExchange post) is to make a script that:

 

  1. Iterates through all paragraphs in the document
  2. If the paragraph has a numbered heading style applied, insert an anchored text frame (with a specific object style applied, which is non-printing and applies the paragraph style the page header text variables are based on)
  3. Populate the text frame with the number of the paragraph (note: the full number, e.g., “5.17.2” – not just “2”)

 

The two first were fairly easy, but I’m stumped for the third. I can easily retrieve the current paragraph’s individual number (“2”) or the formatting applied to the numbering (e.g., “^1.^2.^#^t”), but how to I get the numbers from any previous level? How to I get the full, formatted string “5.17.2”?

 

I suppose I could use Paragraph.convertBulletsAndNumberingToText(), then use GREP replace to delete everything after the numbering, but that seems extremely hacky for something so rather fundamental to a nested list as accessing the fully qualified number. Moreover, convertBulletsAndNumberingToText() can only be applied to an existing Paragraph object and has the bizarre and counterintuitive behaviour of returning undefined, rather than returning the converted string as you’d expect. I’m not sure I’d even be able to duplicate the paragraph into the anchored text frame and textify the numbering at all – the act of duplicating the paragraph might just increase the numbering so I’d end up with “5.17.3” as the text, rather than “5.17.2” …

 

Is there really no simple, sane function to extract this?

Okay, so it seems I was looking through the properties of the Paragraph class too narrowly – I was looking under N for numberingXXX, and it turns out there is a property called Paragraph.bulletsAndNumberingResultText which gives me exactly what I’m looking for (well, nearly – that property also includes the tab that follows the numbering marker, but that should be easy to get rid of).

 

So simple, and yet it took me hours to locate. 😕

TOPICS
Scripting

Views

76

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
Jun 29, 2020 0
Participant ,
Jun 30, 2020

Copy link to clipboard

Copied

Okay, so it seems I was looking through the properties of the Paragraph class too narrowly – I was looking under N for numberingXXX, and it turns out there is a property called Paragraph.bulletsAndNumberingResultText which gives me exactly what I’m looking for (well, nearly – that property also includes the tab that follows the numbering marker, but that should be easy to get rid of).

 

So simple, and yet it took me hours to locate. 😕

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
Reply
Loading...
Jun 30, 2020 1
Most Valuable Participant ,
Jun 30, 2020

Copy link to clipboard

Copied

Did you try retrieving the bulletsAndNumberingResultText property of your paragraph? (See https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Paragraph.html)

 

(Hm. There is no (longer?) the option to simply delete my answer. I see you found it while I was testing it out and searching the right page on Gregor's site ...)

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
Reply
Loading...
Jun 30, 2020 1