• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Page Change Footnote Number Change

Enthusiast ,
Jun 04, 2022 Jun 04, 2022

Copy link to clipboard

Copied

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

TOPICS
How to , Scripting

Views

580

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

Community Expert , Jun 09, 2022 Jun 09, 2022

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:

...

Votes

Translate

Translate
Participant ,
Jun 05, 2022 Jun 05, 2022

Copy link to clipboard

Copied

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.

Votes

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
Enthusiast ,
Jun 06, 2022 Jun 06, 2022

Copy link to clipboard

Copied

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

 

Votes

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
Community Expert ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

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?

Votes

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
Community Expert ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

Sorry I misunderstood. Try this in your main p loop: 

 

for (var i=0; i<p.length; i++) {
    p[i].contents += " " + p[i].contents.match(/\d+/g)[0];
}

 

Votes

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
Enthusiast ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

Thanks for the help Brian. 

I will try the code today and update.

Regards

 

Votes

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
Enthusiast ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

This is the error which I am getting after including the line 

 p[i].contents += " " + p[i].contents.match(/\d+/g)[0];

=============

JavaScript Error!

Error Number: 21
Error String: null is not an object

Engine: main
File: C:\Users\node1\AppData\Roaming\Adobe\InDesign\Version 17.0-ME\en_US\Scripts\Scripts Panel\convert.jsx
Line: 22
Source: p[i].contents += " " + p[i].contents.match(/\d+/g)[0];

=================

 

My code now looks like this:

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);
p[i].contents += " " + p[i].contents.match(/\d+/g)[0];
//app.activeDocument.footnoteOptions.restartNumbering = FootnoteRestarting.PAGE_RESTART;

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

Votes

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
Community Expert ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

The error indicate that there is an instance of p that doesn't contain any digits. You should capture that possibility. Something like this:

var temp = p[i].contents.match(/\d+/g);
if (temp != null) {
  p[i].contents += ' ' + temp[0];
}

 

Votes

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
Enthusiast ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Thanks for the reply Peter.

I tried the code. My for loop now looks like this :

for (var i=0; i<p.length; i++)
{
var temp = p[i].contents.match(/\d+/g);

//alert(temp);
if (temp != null)
p[i].contents += ' ' + temp[0];
}

But when I run the script, there is no response at all as if nothing has happened. I tried printing out the value of  temp and its showing as "null". 

Totally lost now.

 

Votes

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
Community Expert ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Ah, sorry, I forgot what this is all about. Footnote numbers are codes, you can't deal with contents. You'll have to duplicate the footnote 'number', something like this:

 

 

app.findGrepPreferences.findWhat = "~F";
var p = app.activeDocument.findGrep();
for (var i = p.length-1; i >= 0; i--) {
  p[i].characters[0].duplicate (LocationOptions.AFTER, p[i].texts[0].insertionPoints[-1]);
}

 

 

p[i].characters[0] is the footnote number, p[i].texts[0] is the footnote.

Votes

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
Enthusiast ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Thanks for the help Peter.

I will try to fit the code you provided in my code and try to get some
output.

Thanks a ton.

Votes

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
Enthusiast ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Hello Peter,

I used your code.

What your code is doing is making a duplicate footnote entry eg:

On Page 1

11.This is Footnote number one

22. This is Footnote number one

 

On Page 2

11. This is a footnote in page 2

22. This is a footnote in page 2

What I want to achieve is:

 

On Page 1

1. This is footnote number one. 1

2. This is footnote number two. 2

 

On page 2:

1. This is a footnote in page two. 1

2. This is another footnote in page two. 2

 

The number following the footnote should be an actual number not a footnote marker becuase when i copy and paste it somewhere, this is what it shows : <?>

 

Thanks 

Votes

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
Community Expert ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

I should have added that I hadn't tried that code! This one I did try and works as expected:

app.findGrepPreferences.findWhat = "~F";
var p = app.activeDocument.findGrep();
for (var i = p.length-1; i >= 0; i--) {
  if (p[i].parent instanceof Footnote) {
    p[i].parent.texts[0].insertionPoints[-1].contents = ' ';
    p[i].characters[0].duplicate (LocationOptions.AFTER, p[i].parent.texts[0].insertionPoints[-1]);
  }
}

However, as you noted, it copies the footnote number as a code, not as a static number that you can copy and paste somewhere else. The problem is that unlike several other automatic numbers (numbered paragraphs, for instance) you can't get the number as normal text. You have to determine the note numbers by counting the number of notes on each page if you restart numbering on each page, (which is what you're doing by the looks of it), and which gets complicated.

When you say 'when I copy and paste it somewhere', where is that somewhere?

Votes

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
Enthusiast ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Thanks for the reply Peter.

I have been trying to find the number of footnotes per page but all I get
is the total number of footnotes in the threaded frame.

Actually I do not need to copy the number somewhere. I will hide the number
using grep and then later on planning to use that number for
cross-referencing from one footnote to the other.

It can be done manually, in fact I have done it manually, but 500-600 pages
of footnotes manually will be difficult. On top of that,if a footnote is
deleted or added, that will be a problem.

That's the reason I was looking for the script. My original script works
but only if I do not restart the footnote numbering on every page.

In m y original code, I just need to reset the counter back to one when the
footnote on the next page starts with 1.

The code which you provided is awesome and I have kept it safe for future
use. In fact the code which I wrote originally was after studying your
code.

Thanks once again.
Regards

Votes

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
Community Expert ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

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.)

 

Votes

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
Community Expert ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

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.

 

Votes

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
Enthusiast ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

LATEST
Thank you so much Peter. You have been of great help.
I will further fine tune my code in light of the codes which you have
provided.
I am very grateful to you.

Thank you.

Votes

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