8 Replies Latest reply on Sep 24, 2010 4:16 AM by Skempy

    [JS CS4] insertionPoints problem

    Skempy Level 1

      I am trying to delete the text in a paragraph but leave the paragraph mark in place using:

       

      myParagraph.insertionPoints.itemByRange(0,-2).contents = ""

       

      This code works unless the paragraph is the last one in a story in which case the last character is left behind.

       

      Is there an easy way to make this work for any paragraph?

       

       

      Thanks

       

      Simon.

        • 1. Re: [JS CS4] insertionPoints problem
          Peter Kahrel Adobe Community Professional & MVP

          Something like this maybe:

           

          if (myPar.characters[-1].contents = "\r")

             myPar.contents = myPar.contents.replace (/.+/, "\r");

          else

             myPar.contents = "";

           

          Peter

          • 2. Re: [JS CS4] insertionPoints problem
            [Jongware] Most Valuable Participant

            Peter, any idea why this doesn't work?

             

            myPar.contents = myPar.contents.replace (/.+$/, "");

             

            It seems for Javascript the final returns are included in the string, whereas with GREP S&R it leaves them.

             

            (Feel free to call it an "implementation difference".)

            • 3. Re: [JS CS4] insertionPoints problem
              Marc Autret Level 4

              I think this should work in any case:

               

              myPar.contents = myPar.contents.replace(/[^\r]+/,"");

               

               

              @Jongware

               

              myPar.contents = myPar.contents.replace (/.+$/, "");

               

              doesn't work because the regexp /.+$/ matches the whole myPar.contents string, including "\r" which ends any paragraph (except the last one in the story). By the way, /.+$/ is exactly the same as /.+/, since the dot matches any character and the plus is greedy.

               

              In GREP the behavior of .+ is usually different because the single line mode is OFF by default, which means that the dot matches any character except the paragraph end mark. But if you revert the single line modifier in GREP:

               

              (?s).+

               

              then you grab the entire text available*, like /.+/ does in JavaScript RegExp.

               

              * This rule has an exception mentioned by Laurent Tournier in its GREP & InDesign French book: (?s).+ will break on any footnote call (!)

               

              @+

              Marc

              • 4. Re: [JS CS4] insertionPoints problem
                [Jongware] Most Valuable Participant

                Marc: Would the multiline flag 'm' as mentioned on http://www.websina.com/bugzero/kb/regexp.html make my suggestion work?

                 

                (Sorry, busy with sth else & no time to try myself )

                • 5. Re: [JS CS4] insertionPoints problem
                  Marc Autret Level 4

                  Yeah, it's a good idea, but the /m modifier makes ^ and $ break on the "\n" character on Windows platforms (I don't know how it behaves on Mac...).

                  As InDesign uses "\r" in paragraph termination, /.+$/m won't work as expected...

                  • 6. Re: [JS CS4] insertionPoints problem
                    Peter Kahrel Adobe Community Professional & MVP

                    myPar.contents = myPar.contents.replace (/.+$/, "");

                     

                    This was my first try, too, and I was surprised to see that that didn't work. I'm sure it should work: . matches everything except line ends, and surely \r is a line end. As Marc said, it looks as if \n is seen as a line end, \r is not. I've run into that problem before.

                    Marc's solution works fine, though!

                     

                    Peter

                    • 7. Re: [JS CS4] insertionPoints problem
                      Skempy Level 1

                      Peter, Marc, Jongware,

                       

                      Thank you all for your replies, I doubt there could be a better set of Scripting brains involved in my problem.

                       

                      Marc's

                       

                      myPar.contents = myPar.contents.replace(/[^\r]+/,"");

                       

                      almost achieves what I need, but this results in the Paragraph Style of the deleted text being applied to the following paragraph.

                       

                       

                      Simon.

                      • 8. Re: [JS CS4] insertionPoints problem
                        Skempy Level 1

                        This seems to work...

                         

                        if (myPar.characters[-1].contents == '\r') {
                        myPar.insertionPoints.itemByRange(0,-2).contents = ""
                        }

                         

                        else {
                        myPar.contents = ""
                        }

                         

                        A slight modification of Peter's code but leaving the original end of paragraph instead of replacing it (which picks up the formating from the following paragraph).

                         

                        I do, however, like the idea of a Grep replace statement if anyone can get one to work.

                         

                         

                        Thanks

                         

                        Simon.