2 Replies Latest reply on Nov 19, 2015 2:27 AM by The Gothamite

    Move all URLs in Text to Footnotes

    The Gothamite Level 1

      Hi everyone

       

      I have an interesting little issue. Basically, I have a text in which all URLs are displayed in the format <URL>www.example.org<URL> and I want to move those parts - preferrably without the <URL>-tags to footnotes. So I set out to do this and I got quite far. I can create empty footnotes for every occurrence of <URL>(.+?)</URL> but then when I want to move the text, I am stumped on how to define what text should be moved without losing the location of the footnote in relation to the text. So here's what I have:

       

      function href() {

      var curSel = app.selection[0];

      // current selection is what the script is looking through

      var searchTerm = "<URL>(.+?)<URL>";

      // Search term as variable for ease of potential later use

      app.findGrepPreferences = app.changeGrepPreferences = null;

      // Reset search settings

      app.findGrepPreferences.findWhat = searchTerm;

      // Perform search for searchTerm

      var result = curSel.findGrep( true );

      app.findGrepPreferences = null;

      for ( var f = 0; f < result.length; f++ ) {

        //f is defined as 0, but if there are more results than 0, increment f by the number of results

        var curRes = result[f];

        // Defines curRes as the number of results f --> Last occurrence of searchTerm in selection

        var curFootnote = curRes.footnotes.add();

        // Creates empty footnote for last instance of <URL>.+?<URL>

        moveURL.move(LocationOptions.AFTER, curFootnote.insertionPoints[-1]);

      }

      app.findGrepPreferences = null;

      }

      I apologize for the rather silly sounding commenting in it, but I try to make sure that everyone with a bit of JavaScript knowledge knows that I'm doing.

       

      As you can see, var moveURL is undefined. var moveURL would be the function where I define the text that is to be moved. Now, it can't be curSel because that would try to move the entire text into the footnote, which is silly for obvious reasons. curFootnote is also not it, because that generates the footnote. So what am I doing wrong here?

       

      Thanks for your help!

        • 1. Re: Move all URLs in Text to Footnotes
          [Jongware] Most Valuable Participant

          You don't need a moveURL 'intermediate' at all.

           

          The problem occurs because you add a new footnote to the found item (curRes) - which you can see if you add this after the footnote.add line:

           

          curRes.select();exit();
          
          

           

          (then please remove it again, as it's only to show how to check what the problem is).

           

          You can fix this, oddly enough, by adding the footnote before the targeted text:

           

          var curFootnote = curRes.insertionPoints[0].footnotes.add();
          
          

           

          If you check with the 'select' trick, you will see that the found text 'curRes' still only consists of the URL part, and the new footnote appears right before it, and not inside. When adding a note 'at the end', using

           

          var curFootnote = curRes.insertionPoints[-1].footnotes.add();
          
          

          you will find the note becomes part of 'curRes' and so you cannot move its text anymore into that same note.

           

          With that done, it's a breeze to move the found text into the footnote, but then you still have to remove the URL markers. There are several approaches but I would do that by copying only the relevant text into the note, then removing the entire found text in the document, like this:

           

          curRes.characters.itemByRange(6,curRes.characters.length-5).duplicate(LocationOptions.AFTER, curFootnote.insertionPoints[-1]);
          curRes.remove();
          
          
          • 2. Re: Move all URLs in Text to Footnotes
            The Gothamite Level 1

            Wow. That worked perfectly! Thank you so much. And thanks for making that website of yours that has helped me so many times before. You're a great asset to the community. Thank you!