Skip to main content
Bedazzled532
Inspiring
June 5, 2022
Answered

Page Change Footnote Number Change

  • June 5, 2022
  • 1 reply
  • 1691 views

Following is my code which adds a number same as footnote number at the end of the footnote. I need that for some reason. This code works fine if in Document Footnote Options, I have not selected to change the footnote number on every page. But when I select to change the footnote number on every page option in InDesign, this code looses its purpose. I need to write something which resets the counter back to 1 on the next page. I have 27 pages each containing one text frame and 27 pages are threaded together. Each frame has at least 1 multi line footnotes. Please help.

 

convert();
function convert() {
myDocument = app.activeDocument;
var pageCount = myDocument.pages.length;

//Reset all the Grep preferences first
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

//Enter the GREP pattern to search
app.findGrepPreferences.findWhat = "~F~>\\K(.+)";

//Execute the Find GREP
var p = app.activeDocument.findGrep();

//If GREP does not return any matches, alert the user
if (p.length == 0) { alert('GREP did not return any matches'); exit()}

//Iterate through the found items and add a number same as footnote at the end
//A space is added before a Number
for (var i=0; i<p.length; i++)
p[i].contents += " " + (i+1);

//Reset all the Grep preferences for next use
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
}

 

The code is working perfectly fine but when I change document footnote option to change the footnote number on every page, this code needs to be reset which I am not able to do. Any help will be appreciated.

Thanks

This topic has been closed for replies.
Correct answer Peter Kahrel

You can get footnote-count on a page simply by checking the number of footnotes in the text frame:

myTextFrame.footnotes.length

If there are several text frames and you don't know which frame the notes are, use something like this:

myPage.textFrames.everyItem().footnotes.length

(Assuming that there is only one frame with notes.)

 


I was intrigued. I assume you apply a character style to the duplicated footnote numbers in order to hide them. And I assume that you apply that style with a GREP style. When you add or remove any footnotes and want to renumber them, first delete all duplicated numbers, then (re)number the notes. In my example the numbers are added prefixed by ## so that you can later find them them unambiguously. 

 

If you don't use a GREP style then you can delete those numbers in some other way.

 

As follows:

d = app.documents[0];
app.findGrepPreferences = app.changeGrepPreferences = null;

// Delete any hidden footnote numbers. They're
// at the end of the note prefixed by '##'
// 'hidden footnote number' is a character style applied by a GREP style

app.findGrepPreferences.findWhat = '##\\d+$';
app.findGrepPreferences.appliedCharacterStyle = d.characterStyles.item ('hidden footnote number');
d.changeGrep();

// Find the story with footnotes
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '~F';
fn = d.findGrep();

// Get a handle on the story's text frames
frames = fn[0].parentStory.textContainers;

// If a frame contains footnotes, add their number at the end,
// prefixed by '##' so that we can find those numbers unambiguously later on

for (i = frames.length-1; i >= 0; i--) {
  notes = frames[i].footnotes.everyItem().getElements();
  for (j = 0; j < notes.length; j++) {
    notes[j].texts[0].insertionPoints[-1].contents = '##' + String(j+1);
  }
}

 

P.

 

1 reply

Inspiring
June 5, 2022

If i am interpreting this correctly, one possible workaround is to modify your existing script to find the added "space+digit+$", delete it, then run the original script again. You can also further restrict the findgrep to size/font/style, etc.
I think it's easier just to delete the entries manually, and run your script again.
I would suggest you use a special space character the this time (n-space or two half-space, so that you can actually delete them with indesing grep find and change. No script required.

Bedazzled532
Inspiring
June 6, 2022

Thanks for the reply.

I am giving you an example which will help you understand it better.

 

Secanrio 1

Page 1 has 3 footnotes - 1, 2 and 3

Page 2 has 5 footnotes - 1, 2, 3, 4 and 5.

 

My script is not working in the above scenario.

 

The scenario in which my script is working flawlessly is:

Scenario 2

Page 1 has 3 footnotes - 1, 2 and 3

Page 2 has 5 footnotes - 4, 5, 6, 7 and 8.

 

I need solution for Scenario 1.

Secondly, it can be done manually, however, Number of pages in the book is approx 500 pages. 

 

I know if the script above is modified it will work, however, I have no clue as I am a starter in Scripts.

Thanks

 

brian_p_dts
Community Expert
Community Expert
June 7, 2022

To restart footnote numbering on every page on the document, this one line code would work: 

 

app.activeDocument.footnoteOptions.restartNumbering = FootnoteRestarting.PAGE_RESTART;

 

You could incorporate that into the code if I'm understanding correctly?