3 Replies Latest reply on May 6, 2009 12:33 PM by Peter Kahrel

    Converting Endnote to Footnote

    sudar1983_14 Level 2

      Hi All,

       

      I have a problem with converting endnote to footnote, My idea is

       

      1. Get all the endnotes to the array

      2. Get all endnote references

      3. Is it match then im going to insert footnote.

       

       

      var endParas=new Array();

      var oldContents="";

       

      aDoc=app.activeDocument;

       

      for(var pg=0; pg < aDoc.pages.count(); pg++)

      {

      var page=aDoc.pages.item(pg);

      for(var tf=0; tf < page.textFrames.count(); tf++)

      {

      var textFrame=page.textFrames.item(tf);

      for(var para=0; para < textFrame.paragraphs.count(); para++)

      {

      myPara=textFrame.paragraphs.item(para);

      if (String(myPara.appliedParagraphStyle.name).match(/Endnotes/))

      {

      myContents=myPara.contents;

      if (myContents != oldContents && myContents !="")

      {

      if (String(myContents).match(/\d+\t/ig))

      {

      endParas.push(myPara);

      }

      oldContents=myPara.contents;

      }

      }

      }

      }

      }

       

      app.findGrepPreferences=app.changeGrepPreferences=null;

      app.findGrepPreferences.findWhat="\\d+";

      app.findGrepPreferences.position = Position.superscript;

      var matchedItems=aDoc.findGrep();

       

      if (matchedItems.length == endParas.length)

      {

      var myFootnotes=new Array();

      for (var mtch=0; mtch < matchedItems.length; mtch++)

      {

      matchedItems[mtch].select();

      app.selection[0].contents="";

      myFoot=app.selection[0].insertionPoints[0].footnotes.add();

      endParas[mtch].texts[0].move(LocationOptions.after, myFoot.insertionPoints[myFoot.insertionPoints.length-1]);

      }

      }

      else

      {

      alert('Endnote citations and notes not equal, so it will not run');

      exit(0);

      }

       

      What was the problem here is, when im inserting the footnote 'endParas' array automatically changing the paragraphs, so we won't get correct endnote paragraph.

       

      Anybody have other ideas, pls let me know.

       

       

      Regards,

      sudar

        • 1. Re: Converting Endnote to Footnote
          [Jongware] Most Valuable Participant

          The data in your endParas array can be considered a pointer to text in InDesign, rather than 'holding the text location' or something. If you change text before endArray[0], that element location and all after it are invalidated. If you insert something between endArray[0] and endArray[1], everything after that is invalidated. The array elements simply do not point anymore to the original location -- they are not updated automagically.

           

          The solution is to change the text pointed to by the endParas array from last item to first. You can safely change the last item without disturbing the pointers before it. Next you can change the item before the last one, and so on.

           

          Notice that you can only do this trick once with your existing array. If you need to do something else to "the same array" (quoted, because you might already guess it's no longer "the same"), you need to rebuild it from scratch.

          • 2. Re: Converting Endnote to Footnote
            sudar1983_14 Level 2

            Hi,

             

            We can reverse the loop, then also, the same problems occurs, any others ideas?

             

             

            Regards,

            sudar

            • 3. Re: Converting Endnote to Footnote
              Peter Kahrel Adobe Community Professional & MVP

              I would go about thi as follows:

               

              1. Collect all notes, looking for evrything formatted with a note style and headed by a number. This looks a bit like what you're already doing:

               

              app.findGrepPreferences = null;

              app.findGrepPreferences.findWhat = "^\\d+";

              app.findGrepPreferences.appliedParagrapStyle = app.documents[0].paragraphStyles.item ("Endnote");

              notes = app.documents[0].findGrep();

               

              2. Collect the note references, again more or less the way you do:

               

              app.findGrepPreferences = null;

              app.findGrepPreferences.findWhat = "^\\d+";

              app.findGrepPreferences. position = //superscript

              references = app.documents[0].findGrep();

               

              (Check, as you do, that there are as many notes as there are references.)

               

              3. Starting at the end, create a note at each "references" and move a "notes" to it.

               

              As Jongware mentioned, you must start working from the end because (a) creating footnotes in the text disturbs the "references" collection, and moving "notes" to the newly created footnotes upsets the "notes" collection.

               

              In your script, you create an array of notes but there's not really any need for it -- your script creates the array but doesn't do anything with it.

               

              Peter