19 Replies Latest reply on Apr 30, 2012 12:45 AM by Eyenan.

    IDCS5 JS: Problem with 'captured' text losing formatting

    Iain Robinson Level 1

      Hi all

       

      We use Peter Kahrel's excellent script to convert footnotes to dynamic endnotes in IDCS5 after which we use some of our own JS to grab that text, put it in an orange box and then delete the original endnotes text. This works fine but have just realised that the formatting (italics) that was in the original endnotes text does not survive being moved to the new text box. The italics is even protected by a character style.

       

      Our code;

      //searches for endnotes, puts them in a orange box and adds the "Endnotes" heading
      app.findGrepPreferences = null;
      app.changeGrepPreferences = null;
      app.findChangeGrepOptions.includeFootnotes = true;
      
      var myDoc = app.activeDocument
      var myPara = myDoc.paragraphStyles.item ("Endnotes");
      
      // Find the text & capture its contents to a variable
      app.findGrepPreferences.appliedParagraphStyle = "Endnotes"
      origText1 = app.activeDocument.findGrep (); 
      
      //check if any Endnotes are found, nothing happens if not
      if(origText1.length!=0)
      {
      newText1=origText1[0].contents;
      
      //adds new text frame on 1st page of doc
      var myTextFrame1 = myDoc.pages.item(0).textFrames.add();
      myTextFrame1.geometricBounds = ["6p", "6p", "36p", "36p"];
      
      // Apply the objectStyle to frame
      var myOStyle = myDoc.objectStyles.item("Endnote box");
      myTextFrame1.applyObjectStyle(myOStyle); 
      
      //add captured text to text box
      myTextFrame1.contents = ("Endnotes\r" + newText1);
      
      var myDelete = myDoc.paragraphStyles.add ({name: "Endnotes1", basedOn: myPara});
      
      // Loop to apply paragraph style to text
      var length = myTextFrame1.paragraphs.length
      
      for (var i =0; i<=length-1; i++ )
      {
      //apply para style to new frame contents
      var myStory = myTextFrame1.paragraphs[0].parentStory
      myStory.paragraphs[i].appliedParagraphStyle = "Endnotes1"
      }
      
      //removes original text
      app.findGrepPreferences.appliedParagraphStyle = "Endnotes"
      app.changeGrepPreferences.changeTo="";
      app.documents.item(0).changeGrep();
      
       // remove Endnotes1 paragraph style and replace with Endnotes style
      myDelete.remove (myPara)
      }
          
      else
      {
       }
      

       

      As you can see we capture the text to a variable using GREP and then place that in a new box (preceded by a heading). We make a copy of the para style "Endnotes", use that to style the copied text, delete everything styled with the original style (the original wording) and then delete the new style replacing it wiith the original style so the copied text goes back to para style "Endnotes" again. I'm sure there's a better way but it works for what we want - at least I thought it did.

       

      Any thoughts gratefully appreciated. I searched the forum but couldn't find anything relevant. What am I missing?!

       

      thanks,

      Iain

        • 1. Re: IDCS5 JS: Problem with 'captured' text losing formatting
          Marc Autret Level 4

          Hi Ilain,

           

          The captured text loses formatting because of:

           

          newText1=origText1[0].contents;

           

          This set newText1 to a simple JS String (all formatting are lost). Instead you should directly move or duplicate the Text element(s).

           

          @+

          Marc

          • 2. Re: IDCS5 JS: Problem with 'captured' text losing formatting
            Iain Robinson Level 1

            Thanks for the prompt reply Marc - can you suggest where I start looking to find out how to use that method?

             

            thanks,

            Iain

            • 3. Re: IDCS5 JS: Problem with 'captured' text losing formatting
              Marc Autret Level 4

              Why don't you simply "move" the found texts to the endNote box?

               

              I suggest you the following approach (not tested, draft code):

               

               

              function moveEndnotesInBox()
              //--------------------------------------
              // Searches for endnotes, puts them in a orange box and adds the "Endnotes" heading
              {
                  // Preset findGrep
                  // ---
                  app.findGrepPreferences = null;
                  app.findChangeGrepOptions.includeFootnotes = true;
                  app.findGrepPreferences.appliedParagraphStyle = "Endnotes";
              
                  // Find the text(s)
                  // ---
                  var doc = app.activeDocument,
                      foundTexts = doc.findGrep(),
                      i = foundTexts.length;
              
                  // If no 'Endnotes' text is found, return
                  // ---
                  if( !i ){ alert("No endnote found"); return; }
              
                  // Creates a new text frame on 1st page w/ specific
                  // bounds, and preset contents
                  // ---
                  var tf = doc.pages[0].textFrames.add(
                      {
                      geometricBounds: ['6p', '6p', '36p', '36p'],
                      contents: "Endnotes\r"
                      });
              
                  // Preset obj and parag. style
                  // ---
                  tf.appliedObjectStyle = doc.objectStyles.itemByName("Endnote box");
                  tf.parentStory.paragraphs.everyItem().appliedParagraphStyle = "Endnotes";
              
                  // Backup the last insertion point index
                  // ---
                  var    ips = tf.parentStory.insertionPoints,
                      ipx = ips.length - 1;
              
                  // Loop through foundTexts and move texts to tf
                  // (IMPORTANT: from the last to the first item to preserve Text indices)
                  // ---
                  while( i-- )
                      {
                      foundTexts[i].move(LocationOptions.AFTER, ips[ipx]);
                      }
              }
              
              moveEndnotesInBox();
              

               

              Of course you still need to deal with possible missing paragraph returns and/or 'ghost' footnote markers. I don't know exactly the context, but this doesn't seem complicated.

               

              Hope that helps.

               

              @+

              Marc

              • 4. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                Iain Robinson Level 1

                Thanks Marc - I can follow that. Never used "move" before.

                 

                Thanks again for your time,

                Iain

                • 5. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                  Marc Autret Level 4

                  Hey Iain,

                   

                  It seems you have found the "Correct Answer", good for you ;-)

                   

                  @+

                  Marc

                  • 7. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                    Marc Autret Level 4

                    Thanks, you are a magician. Hey, while you're at it, why not give me credit for 1000 extra points

                    • 8. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                      Iain Robinson Level 1

                      I have another question about the code snippet that Marc Autret very kindly did for me. It worked perfectly but I need to fully understand it so I can use the method in other instances. I understand what move is doing but the while loop confuses me. I don't understand what (i--) is doing - I thought that part of the loop is the condition, which must be true. What you have there is i being decremented? (i is the length of the found text) I guess that is what it makes go backwards but I'm confused about the syntax (again).

                       

                      Also, can anyone remind me why it is best to do the moving of the paragraphs backwards? I guess that after it is made (before the text is moved into it) tf has two insertion points - one at the beginning of the para and one after the paragraph marker? Which is why ipx = ips.length -1?

                       

                      many thanks,

                      Iain

                      • 9. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                        Iain Robinson Level 1

                        Okay, I think I can appreciate now why it is good to work from the back when processing text (although it may not be strictly necessary in this case?) but I'm still curious about the (i--) "condition" of the while loop. I can see what the result of the loop is (and it works!) but I could never have written that myself as according to all sources I have read or seen the condition has to be a statement which is 'true' - what we have here is an decrement instruction! Is this some undocumented shortcut that I don't know about? I did search on Google for while (i--) but searching for parentheses on Google is not fun.

                        • 10. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                          Harbs. Level 6

                          i-- decrements i by one. i-- is a post-decrement, so the new value is only assigned after the "if" check. If che ks for a boolean. Any number other than 0 is evuivalent too true, so the check passes until i is a value of 0.

                           

                          Harbs

                          1 person found this helpful
                          • 11. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                            Iain Robinson Level 1

                            Thanks Harbs. There is no "if" in the while loop - do you mean it acts like an "if"? So in other words what while (i--) means is;

                             

                            Run what is in the while loop, decrementing i by 1 each time, until i=0?

                             

                            If so that's a neat shortcut and useful to know.

                            • 12. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                              Harbs. Level 6

                              Sorry. I was a bit distracted while I wrote that response standing by a bus stop responding from my iPad...

                               

                              Yes. Replace "if" with "while". while() tests for a boolean the same way if() does...

                               

                              Harbs

                              • 13. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                                Iain Robinson Level 1

                                Ahh - thanks. Isn't technology wonderful?! If I stop to think how connected everyone is it blows my mind a little bit. It's like being in a sci-fi film. Not necessarily a good one though!

                                 

                                Hope you're going somewhere nice!

                                • 14. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                                  Harbs. Level 6

                                  Iain Robinson wrote:

                                   

                                  Hope you're going somewhere nice!

                                  Thanks. It's always nice to go home...

                                  • 15. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                                    [Jongware] Most Valuable Participant

                                    To expand a bit on the i-- (or --i) stuff:

                                     

                                    The expression inside the while (..) parentheses can be just about anything. The i-- works, because this line

                                     

                                    i--;

                                     

                                    in itself is valid Javascript, and the 'result' of an expression is the value that was last assigned. Hence, it is a common mistake to write something like this

                                     

                                    while (i = 1)

                                    ...

                                     

                                    and expect the while-loop to stop when i does not equal '1'. I can tell you right away it always does and the loop will never stop! Even if i got changed in the code inside the while-loop, the statement-on-its-own

                                     

                                    i = 1

                                     

                                    resets it to '1', and the result '1' is returned to the 'while'.

                                     

                                    The reverse is true as well. The above line should have read

                                     

                                    while (i == 1)

                                    ...

                                     

                                    which returns 'true' or 'false' as its value; and the expression

                                     

                                    i == 1;

                                     

                                    on its own is valid Javascript, but it has no use on its own because you don't "do" anything with the result. This line

                                     

                                    i--;

                                     

                                    on its own is useful because the specific effect of the '--' operator is that the variable gets decremented and the new result stored.

                                     

                                    Also:

                                    Consider the difference between pre-decrement and post-decrement. --i decrements i and returns the result. i-- "returns the result" and then decrements the variable -- the value that gets tested for is the original value of i!

                                    You can see this in practice with these:

                                     

                                    while (i--)

                                      $.writeln ("Hello");

                                     

                                    and

                                     

                                    while (--i)

                                      $.writeln ("Hello");

                                     

                                    How many hello's do you get on each, with i=2? With i=1? With i=0?

                                    1 person found this helpful
                                    • 16. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                                      Iain Robinson Level 1

                                      Thanks Jongware - that is a good description. Your example did indeed show what i-- did, and illustrate the difference between it and --i. This is fairly advanced stuff for me!

                                       

                                      PS Thanks for crashing my ESTK!

                                      • 17. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                                        [Jongware] Most Valuable Participant

                                        Iain Robinson wrote:

                                         

                                        PS Thanks for crashing my ESTK!

                                         

                                        I bet it was the ole' "i=0" trick that did that Fortunately, it's (usually) harmless to make your ESTK crash.

                                         

                                        (On counting down from 0 you first get to -1, which doesn't stop the while-loop. Then it merrily trails off to -2, -3, ... -9,999,999,999 ... and then it dives into the Unknown Realms of Integer Overflow, where the numbers get too large or too small to comfortably fit into the allocated memory space; or -- possibly worse -- the Unknown Realms of Promotion to Real Number, and those can go down all the way to minus 10 to the power 300 -- a -1 followed by 300 zeros -- so it can take a while.)

                                        • 18. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                                          Iain Robinson Level 1

                                          Yes, no harm done. Judging by your description of how far it was counting it is no surprise it locked up.

                                          • 19. Re: IDCS5 JS: Problem with 'captured' text losing formatting
                                            Eyenan.

                                            Hi Harbs,

                                             

                                                           The above code was working fine for moving to textframes. But its not working for moving to footnote. Pls do the needful