14 Replies Latest reply on May 17, 2013 2:19 PM by Peter Kahrel

    How to write data at the end of doc in reverse order?[CS6-jsx]

    Beliakov Level 1

      I need to move all my spec texts entries at et end of doc. To keep correct reference in all found entries i have to process in reverse order

      for (i = mFound.length-1; i>=0; i--){

       

      ...etc

       

      but can't to think up, how to place entries in write order at the end.

       

      String:

      mStory.insertionPoints.item(-1).select();

      app.paste();

       

      does not work properly (of course). It makes this:

       

      10.

      9.

      8.

      7.

      6.

      ...etc

      How to get

      1.

      2.

      3.

      4.

      ...etc

      ?

        • 1. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
          milligramme Level 3

          Hi,

          I think what you are doing is the code below.

          you should fix using

          findGrep(true) => true is 'reverse order' option

          and move()

          var dummy = "Um veles 1.eatiorum et alic tem 2.quibusda nit volest, to corion conserum rem ipsanitate verores sequiduciat la demquis 3.inctorem ellestem reptis a et offic tem quidel earcide bissere mpellorerit ipsaecearios 4.volorrovid earchit omnitat atquatus aut quam nimet idelluptatum quis aut lam quo 5.ipsapiciis se simus autem fugias esto et et volupti to eum rem reius nobitatur aut evelis rae arumque lanihilicia sapitiam volorep ellupta volupta aut qui rendia doluptusam am necatur? Em aut abo. "
          var doc = app.documents.add();
          var tf = doc.textFrames.add({geometricBounds: ["10mm","10mm","100mm", "80mm"], contents:dummy});
          var st = tf.parentStory;
          
          var find_grep_obj = {
            findWhat : "\\d+\\.[^\\s]+"
          };
          
          with(app.findChangeGrepOptions){
            includeFootnotes            = false;
            includeHiddenLayers         = false;
            includeLockedLayersForFind  = false;
            includeLockedStoriesForFind = false;
            includeMasterPages          = false;
            kanaSensitive               = true;
            widthSensitive              = true;
          }
          
          var match = grep_find(doc, find_grep_obj);
          
          function grep_find(target_obj, find_grep_obj){
            app.findGrepPreferences = NothingEnum.nothing;
            app.findGrepPreferences.properties = find_grep_obj;
            var result = target_obj.findGrep(true); //=> reverse order [..., "3.inctorem","2.quibusda", "1.eatiorum"]
            app.findGrepPreferences = NothingEnum.nothing;
            return result;
          }
          var i = match.length;
          while (i--) {
            st.insertionPoints[-1].contents = "\r";
            match[i].move(LocationOptions.AT_END, st);
          }
          

           

          thank you.

          mg

          • 2. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
            Beliakov Level 1

            Yes, param True is good. But unforunately, i need to move entries with numbering its. So numbers appear in reverse order. The mision: to create endnotes list of specially marked texts fragments. I can't use "move" method, because i need to set a number of entrie in place of entrie. So i use "cut-paste" method. And if use a TRUE-key in FindGrep then i got a reverse numbering in text. So far i think to make reverse list, and then in Word make ascending sorting and paste back in Indi-doc.

             

            Thanks for hint, anyway!

            • 3. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
              Vamitul Level 4

              i think this will interest you:

              http://jongware.mit.edu/idcs5/pc_Array.html#reverse

               

              long story short after the find grep or whatever

              myFoundArray.reverse();

              1 person found this helpful
              • 4. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                Beliakov Level 1

                Yes, .reverse() method helped:

                 

                var mStory = app.selection[0].insertionPoints[0].parentStory;

                var aEndList = []; // prepare array

                mFind ="\\{\\{\\{.+?\\}\\}\\}"; // my coding of text being searched (kinda {{{some needed text}}})

                   app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                    app.findChangeGrepOptions.includeFootnotes = false;

                    app.findGrepPreferences.findWhat = mFind;

                 

                var mFound = mStory.findGrep();

                $.writeln(mFound.length);

                for (i = mFound.length-1; i>=0; i--){

                mSel = mFound[i];

                mStr_tmp  = String(i+1) + ". " + mFound[i].contents; //temporary string with added number

                mSel.select();

                app.selection[0].remove();

                 

                //place number of refer instead of refer itself in text

                mSel.insertionPoints[0].texts[0].position = Position.SUPERSCRIPT;

                mSel.insertionPoints[0].texts[0].contents = String(i+1);

                aEndList.push(mStr_tmp);

                }

                 

                aEndList.reverse();

                myString = aEndList.join ("\r");

                //place resorted and numbered list

                mStory.insertionPoints.item(-1).select();

                mStory.insertionPoints.item(-1).contents = myString;

                 

                But the problem arose. Operating with contents looses text formatting. So i used cut-paste method. can i keep formattin by some way in this code?

                • 5. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                  Vamitul Level 4

                  not sure i understand what you are trying to achieve, but i am quite sure you are overcomplicating things. From your code in guess you are trying to do some kind of endnote system? if so, here is a bit of pseudocode it might help you:

                   

                  //set grep find options as desired
                  var myFound =myStory.findGrep();
                  var l=myFound.length;
                  while (l--) //simple iterate backwards trick
                  { 
                  // add the numbering, without changing anything else:
                  myFound[l].insertionPoints[0].position=Position.SUPERSCRIPT;
                  myFound[l].insertionPoints[0].contents=l+1; // javascript does the automatic conversion, so String(l+1) is redundant, but nice
                  }
                  // get the last insertion point of the story: 
                  var myLastIP=myStory.insertionPoints[-1];
                  //search  again
                  var myFound =myStory.findGrep();
                  var l=myFound.length;
                  while (l--) {
                  //move the found stuff AFTER the last insertion point
                  myFound[l].move(LocationOptions.AFTER,myLastIP);
                  }
                  

                   

                  a side note: my "pseudocode" looks suspicious like javascript, however i did not test it, provide no guarantees, take no responsability whatsoever about it.

                  • 6. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                    Laubender Adobe Community Professional & MVP

                    @Vamitul – basically using the texts object retaining formatting is a good concept!

                    However:

                     

                    1. I got an error from the ESTK, because the automatic conversion from number to string cannot take place.

                     

                    l+1 remains a Number object (ESTK 3.8.0.12 testet with InDesign CS5.5)

                    So "contents=l+1" should be:  "contents=(l+1).toString()" or: "contents=String(l+1)".


                    2. In the while loop at the end you are concatenating every found texts in one paragraph (unless the found text has a paragraph sign).


                    3. And the while loop concatenates the found results in the wrong order (exactly reverse from what is needed).

                     

                    Instead I would do the following:

                     

                    1. Iterate the found texts not in reverse!
                    2. Duplicating the found texts one after another to the end of the story  adding consecutive numbers and things (paragraph signs at the end)
                    3. Changing the found texts in reverse order to superscript numbers

                     


                    Uwe

                    • 7. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                      Peter Kahrel Adobe Community Professional & MVP

                      The result of findGrep()/Text() is an array, not a collection, so you have more leeway in moving the objects around. All you need is this:

                       

                      app.findGrepPreferences = null;
                      app.findGrepPreferences.findWhat = /\{\{\{.+?\}\}\}/.source;
                      found = app.documents[0].findGrep();
                      
                      for (i = 0; i < found.length; i++){
                          found[i].parentStory.insertionPoints[-1].contents = '\r' + String (i+1) + '.\t';
                          found[i].move (LocationOptions.after, found[i].parentStory.insertionPoints[-1]);
                      }
                      
                      

                      Peter

                      • 8. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                        Beliakov Level 1

                        Peter, your code, unfortunately, do not place superscript numbers before moving entries.

                        I added another findGrep-cycle (to write associated numbers in text) and change move method for duplicate in first cycle (not showed):

                         

                        app.findGrepPreferences = null;

                        app.findGrepPreferences.findWhat = /\{\{\{.+?\}\}\}/.source;

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

                         

                        if (found.length > 0){

                            for (i = 0; i < found.length; i++){

                                found[i].parentStory.insertionPoints[-1].contents = '\r' + String (i+1) + ". "; // пробел-em

                                found[i].duplicate (LocationOptions.after, found[i].parentStory.insertionPoints[-1]);

                            }

                        // next cycle

                        for (i = found.length-1; i>=0; i--){

                        mSel =found[i];

                        mSel.select();

                        app.selection[0].remove();

                        //ставим номер ссылки вместо самой ссылки

                        mSel.insertionPoints[0].texts[0].position = Position.SUPERSCRIPT;

                        mSel.insertionPoints[0].texts[0].contents = String(i+1);

                            }

                        }

                         

                         

                         

                        Thanks Peter and Laubender and Vamitul!

                         

                        p.s. i guess, my code again not laconic))

                        • 9. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                          Vamitul Level 4

                          Peter.. the Regex to source.. BRILIANT.

                          Wish i would of thoght of that before i wrote this abomination:

                          String.prototype.escapeR = function() {

                            return this.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");

                           

                           

                          }

                          • 10. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                            Beliakov Level 1

                            And last question.

                            Can i remove {{{ and }}} without converting texts in string and loss of formatting?

                             

                            not like this:

                             

                            m1 = /\{\{\{/;
                            m2 = /\}\}\}/;
                             if(mStr_tmp.match(m1)) {mStr_new = mStr_tmp.replace(m1,"")}
                            if(mStr_new.match(m2)){mStr_new = mStr_new.replace(m2,"")} 
                            
                            • 11. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                              Peter Kahrel Adobe Community Professional & MVP

                              Dmitry,

                               

                              Well, I was basically dealing with moving the notes to the end of the story in the right order. But anyway, as to the superscripted numbers, there's no need to select things and remove those selections. It considered safer, as Vamitul too mentioned, to work on objects. To handle the superscripted numbers, I would do this:

                               

                              app.findGrepPreferences = null;
                              // Search just the note contents: lookaround leaves the braces in the text
                              app.findGrepPreferences.findWhat = /(?<=\{\{\{).+?(?=\}\}\})/.source;
                              found = app.documents[0].findGrep();
                              
                              for (i = 0; i < found.length; i++){
                                  found[i].parentStory.insertionPoints[-1].contents = '\r' + String (i+1) + '.\u2003';
                                  found[i].move (LocationOptions.after, found[i].parentStory.insertionPoints[-1]);
                              }
                              
                              // Now search {{{}}}
                              app.findGrepPreferences.findWhat = /\{\{\{\}\}\}/.source;
                              found = app.documents[0].findGrep();
                              for (i = found.length-1; i >= 0; i--){
                                  found[i].contents = String (i+1);
                                  found[i].position = Position.SUPERSCRIPT;
                              }
                              

                               

                              So first move the notes to the end of the story, leaving behind all the triplets of braces. Then, after moving the notes, you do a new search on the sextets of braces. Don't select and remove, simply redefine the contents of what you found and apply any formatting.

                               

                              Peter

                              • 12. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                                Peter Kahrel Adobe Community Professional & MVP

                                That /.../.source trick I got from J. Hawkinson a while ago in this forum. Maybe before your time

                                 

                                P.

                                • 13. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                                  Beliakov Level 1

                                  Eurika! This trick with lookaround is magic...

                                  I worked/debugged with my original script for three days (until headache). And didn't advanced too far, anyway.

                                  But solution appeared mach simpler (though not so obviously)

                                   

                                  Thanks!

                                   

                                  p.s. by the way, in second cycling no need to use reverse counting (i--). I tried the straight count and all do work correctly.

                                   

                                  //~ for (i = found.length-1; i >= 0; i--){
                                      for (i=0; i< found.length; i++){
                                  
                                  • 14. Re: How to write data at the end of doc in reverse order?[CS6-jsx]
                                    Peter Kahrel Adobe Community Professional & MVP

                                    If all your notes are between 1 and 9, OR between 10 and 99 -- then all works well. But if your text contains single- and double-digit note references (e.g. 1 to 40), then you must use 'reverse counting'. Check your document to convince yourself.