How to get full paragraph number as displayed on the page in ExtendScript
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:
- Iterates through all paragraphs in the document
- 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)
- 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?
