9 Replies Latest reply on Aug 21, 2013 5:43 AM by indegn5

    insert or replace last content

    indegn5

      HI Forum,

       

      I do need a little tweak on this script...

       

      Thing is, i want to find the last paragraph, last line, last character and make it as full stop...

      eg.

      •   can do anything if you need;  (";" to replace with ".").

       

      it is doing that,,,

       

      but if there is no ";" or ":" it is replacing the character d....

       

      I want check the last character is ";" or ":" and to replace with "."

       

      if there is no ";" or ":" just add "." at the end..

       

       

      myDoc = app.activeDocument;

       

      app.findGrepPreferences = null;

      app.findGrepPreferences.bulletsAndNumberingListType = ListType.BULLET_LIST;

      var myResults = app.findGrep();

      var oDoc = app.activeDocument;

       

      for(n=0; n< myResults.length;n++){

      var fnd = myResults[n].paragraphs.lastItem().lines.lastItem();

         

                     if(fnd.insertionPoints.itemByRange(-3,-2).contents !== "."){

                     fnd.insertionPoints.itemByRange(-3,-2).contents = "."

                 

      alert(fnd.insertionPoints.itemByRange(-3,-2).contents)

      }

       

      }

       

       

      Many thanks...

        • 1. Re: insert or replace last content
          Jump_Over Level 5

          Hi,

          do mDoc.changeGrep() twice.

          1. for remove last character if ";", ":" or "." (and unwanted space by the way)

          2. for add "." to every end of bullet's para.

           

          so:

          var myDoc = app.activeDocument;
          app.findGrepPreferences = app.changeGrepPreferences = null;
          app.findGrepPreferences.bulletsAndNumberingListType = ListType.BULLET_LIST;
          app.findGrepPreferences.findWhat = "[\\.;:]\\s*$";
          myDoc.changeGrep();
          app.findGrepPreferences.findWhat = "(.)$";
          app.changeGrepPreferences.changeTo = "$1.";
          myDoc.changeGrep();
          app.findGrepPreferences = app.changeGrepPreferences = null;
          

           

          Jarek

          1 person found this helpful
          • 2. Re: insert or replace last content
            Vamitul Level 4

            you can do it with just one grep actualy (just the grep, no escaping):

            find: (.)[\.;:]?\s*$

            change: $1.

            • 3. Re: insert or replace last content
              indegn5 Level 1

              Thanks to both the G's....

               

              everything is superb & fantastic, still my request is here.....

               

              I do want to change fullstop only to the bullet of last paragraph. and rest of the bullets are like

               

              Jareks' help!   app.findGrepPreferences.findWhat = "[\\.;:]\\s*$";  // superb

              Vam's help!  find: (.)[\.;:]?\s*$ // superb

               

              Many thanks for both the G's.....

              • 4. Re: insert or replace last content
                Vamitul Level 4

                whoo.. well that is a bit more complicated.

                For each

                result you have to test first

                if the next paragraph bulletsAndNumberingListType is not ListType.BULLET_LIST.

                if so, myResult[i].changeGrep();

                 

                Remember always to iterate your array from the back. also there are quite a bit of catches. for example if the list is the last paragraph in the story, using paragraphs.nextItem() will return a invalid object. also of notice is the fact that the"nextItem()" method on colections is horribly slow, so for a large document you ether have to writhe your own method of getting the next paragraph, or have tons and tons of patience.

                 

                if this is a one time use only script or even a rarely used script, i say it's just not worth the trouble.

                • 5. Re: insert or replace last content
                  indegn5 Level 1

                  Thanks Vam,,

                   

                  Could you help me to select, the last content of the paragraph.

                   

                  as it tried above...

                   

                  but the problem is ,,, is replacing the last text if there is no paragraphs after bullet and numbering paragraphs (counting -3, -2)

                   

                  works correct, when there is a paragraph after bullet and numbering paragraphs..

                   

                  so how can i mentioned ) .itemByRange(-3,-2).(-2, -2), if there is no paragraph after bullet... as you already said.. next paragraph. option....

                   

                  thanks vam...

                  • 6. Re: insert or replace last content
                    Vamitul Level 4

                    ah.. could you restate that, please? I don't understand.

                    • 7. Re: insert or replace last content
                      indegn5 Level 1

                      Sorry Vam for the confusion...

                       

                       

                      this is what i mentioned....

                       

                      for(n=0; n< myResults.length;n++){

                      var fnd = myResults[n].paragraphs.lastItem().lines.lastItem();

                         

                                     if(fnd.insertionPoints.itemByRange(-3,-2).contents !== "."){  // if there is a paragraph below the bullet paragraph, it works fine... if not it replaces the last character with"."

                                     fnd.insertionPoints.itemByRange(-3,-2).contents = "."

                                 

                      alert(fnd.insertionPoints.itemByRange(-3,-2).contents)

                      }

                       

                      }

                       

                       

                      eg. • vamitul;

                            • vamitul;

                            • vamitul;                      // now the semicolon  of the last paragraph will be replaced with "."  itemByRange(-3,-2).

                      Dummy text paragraph of this what when dummy text. (paragraph below the last bullet).

                       

                       

                      eg2. • vamitul;

                              • vamitul;

                              • vamitul;

                              • vamitul;    // if there is no paragraph below...  now character "l" is replaced with "." itemByRan(-3,-2) goes to l and change it......

                       

                      Again sorry if im confusing..... vam..

                      • 8. Re: insert or replace last content
                        Vamitul Level 4

                        there are multiple problems with your code.

                        For one, what if you don't have any punctuation at the end of the list? per your example:

                        • vamitul;

                              • vamitul;

                              • vamitul                      /// your code will change this line to vamitu.

                        Dummy text parag

                         

                         

                        as for your example, you have to test if that paragraph is the last one in the story (for example myResults[n].parentStory.paragraphs.nextItem(myResults[n].paragraphs[0]).isValid  will test if there is another paragraph after your found one). if it is the last one, you will have to treat it as a special case and adjust the insertionPoints range.

                        also using insertionPoints is not the most fortunate idea.. (see my point above). instead do a changeGrep() on the found text.

                        • 9. Re: insert or replace last content
                          indegn5 Level 1

                          Superb Vam & Jarek!....

                           

                          I 'll test it and let me open another thread..... for the course of benefit of the forum...

                           

                          Thanks Vam..!!!! excellent reply.

                           

                          Thanks Jarek... for the quick approachable hint.....