5 Replies Latest reply on Mar 20, 2014 9:48 PM by RobertKyle

    Commas before & after selection

    GuidoBenigni

      I'd like to create a script to simply add commas before and after a selected text (considering of course the space to add after the comma before).

      Something like:

       

      get selected text

      add ", " before selection

      add"," after selection

       

      Is this simple enough?

      Thank you,

      Guido

        • 1. Re: Commas before & after selection
          Jump_Over Level 5

          Hi,

           

          To simply add simply run this 1-liner:

          app.selection[0].contents = ", " + app.selection[0].contents + ",";
          

           

          However it is far away from a smart solution...

           

          Jarek

          • 2. Re: Commas before & after selection
            GuidoBenigni Level 1

            Thank you, this is ALMOST all I need, except that InDesign adds also a space BEFORE the first comma.

             

            In the end you have:

            "some text , some more text, even more text".

             

            Any simple way to get rid of that little, insignificant, typographically awful space before the first comma?

             

            Thx again,

            g

            • 3. Re: Commas before & after selection
              Jump_Over Level 5

              Hi,

               

              That's why I wrote is no smart. InDesign does not add additional space - it was there before since your selection is "from 1st letter to the last", I guess.

               

              Select text including space before and add pure "," on both sides.

               

              To tell the true your goal is not to add something "before" and "after" selection but insert "," before 1st character on the left side of selection (if it is a space) and add "after".

               

              Jarek

              • 4. Re: Commas before & after selection
                pixxxel schubser Level 5

                GuidoBenigni wrote:

                 

                … Any simple way to get rid of that little, insignificant, typographically awful space before the first comma? …

                 

                Perhaps you can try something like this:

                 

                var con = app.selection[0].contents;
                con = "," + con + ",";
                app.selection[0].contents = con;
                var conPar = app.selection[0].lines[0].contents.replace (" ,",",");
                app.selection[0].lines[0].contents = conPar;
                

                 

                Have fun

                 

                • 5. Re: Commas before & after selection
                  RobertKyle Level 2

                  Since you can't depend on users carefully selecting what you want, my idea would be to first insert a comma and a space at both ends of the selected text. It works from right to left because the left-end comma moves the right-end insertion point

                   

                   

                  var mySelection = app.selection[0];

                  mySelection.insertionPoints[-1].contents=", ";

                  mySelection.insertionPoints[0].contents=", ";

                   

                  // then clean up any resulting messiness

                   

                  var myStory = mySelection.parentStory;

                  app.findGrepPreferences.findWhat="\\s+,"; // any number of spaces + a comma

                  app.changeGrepPreferences.changeTo=","; // a comma with no spaces

                  myStory.changeGrep();

                  app.findGrepPreferences.findWhat=",\\s+"; // a comma followed by an number of spaces

                  app.changeGrepPreferences.changeTo=", "; // a comma followed by only one space

                  myStory.changeGrep();

                   

                  A real grep expert might be able to do this more efficiently