This sample file was designed to generate a Table of Contents from a sub summarized report.
In FileMaker there is no inherent way of knowing what sub summary part exists on which page as this information is only displayed when in preview mode.
There are two versions of this solution one with JSON and one without - I over engineered the first one with JSON - and then revised it to remove JSON - as it wasn't necessary.
How this technique works.
On the report layout in the header and footer there are some button objects each has a Hide object when calculation that declares a $variable by use of the Let function that evaluates when a script is running.
Once setup the script uses the $hState, $hPage, and $hRec and compares them to the same variables in the footer - to determine if a summary part spans multiple pages.
In order for this to work you have to be in preview mode and the script must pause for the at most a second i enter .001 but its somewhere between that and a full second.
This gives the script enough time to evaluate the variables, and then it loops thru testing each record it finds on the page by comparing the $hRec and the $fRec which are the starting record number and ending record number on that page and then loops thru each page.
After a bit of transformation of the data it generate a table of contents by use of a Virtual List. Then it computes the how many pages the Table of Contents there are and offsets this value for every page of the report then it will output a PDF to the desktop firstly the Table of Contents then append to that PDF the report itself.
Since this technique relies on preview mode - it can't be used on server (server can create pdfs but doesn't have preview mode so can't get the page number.)
Because it also pauses for each record it would not be ideal for super large report.