33 Replies Latest reply on May 17, 2016 9:34 AM by briank83163125

    CS4 script for converting metric values to US, and adding in a table?

    wamboyil

      I have a document that's over 300 pages, containing tables that have columns of numbers representing measurement lengths in millimeters (mm). My client would like me to add corresponding US lengths (in inches, to two decimal places) next to each mm value, in parenthesis, and also in a smaller font size (if possible). For example, if a column in the table looks like this:

       

      10

      20

      45

      15

      10

      90

      55

       

      I need to end up with this:

       

      10 (.39)

      20 (.79)

      45 (1.77)

      15 (.59)

      10 (.39)

      90 (3.54)

      55 (2.17)

       

      Needless to say, for a 300 page document with tables on almost every page, it could take some time to do this all manually! Is there any script (or plugin) that would automate this process for me? Ideally the script (or plugin) would need to do both the mm to in. conversion, AND place the converted value next to the original value, in parenthesis. Any suggestions would be greatly appreciated! Thanks!

        • 1. Re: CS4 script for converting metric values to US, and adding in a table?
          Shonkyin Level 3

          Hi,

           

          Use below code and it will help you.

           

          var myDoc =  app.activeDocument;

          app.changeGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences.findWhat = '\\d+';

          app.findGrepPreferences.appliedParagraphStyle = 'insert your paragraph style';//insert your paragraph style applied on digits

          app.findGrepPreferences.appliedCharacterStyle = 'insert your character style';//insert your character style applied on digits or remove this line if no character style applied

          var myFound = myDoc.findGrep();

          app.changeGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences = NothingEnum.nothing;

           

          for (a=myFound.length-1; a>=0; a--)

          {

          myFound[a].pointSize = 10;

          myFound[a].contents = myFound[a].contents + " (" + (parseInt(myFound[a].contents)*2) + ")"//replace your formula with "(parseInt(myFound[a].contents)*2)"

          }

           

           

          Shonky

          • 2. Re: CS4 script for converting metric values to US, and adding in a table?
            wamboyil Level 1

            Thanks for the response...unfortunately I'm a scripting newbie. I know how to get script code into a text file, save it out as java script, install it into the scripts panel folder, and run it in InDesign, but I'm no good with the coding part of it. I've decided to simplify (hopefully) my request. Forget about changing the font size, paragraph style, etc. All I need to do is take the current numerical value in the table cell, divide it by 25.4 (that's the conversion for mm to inches) and then display it (only to two decimal places) NEXT TO the original value (with a space after it), in parenthesis.

             

            For example: current cell = 100, cell after running script on it = 100 (3.94).

             

            If anyone can generate a script for this quickly and easily, they would earn tons of good karma points!

             

            PS: I'm assuming that to run a script such as this, I would first select all text / table cells that I want to perform the script on, THEN run the script...is this correct?

             

            Thanks again for any help!

            • 3. Re: CS4 script for converting metric values to US, and adding in a table?
              Shonkyin Level 3

              wamboyil wrote:

               

              PS: I'm assuming that to run a script such as this, I would first select all text / table cells that I want to perform the script on, THEN run the script...is this correct?

               

              Thanks again for any help!

               

              Its not compulsory that you select text and run script.

               

              Script need some condition to search your digits.

               

              1. What Paragraph Style applied on digits.

              2. What Character Style applied on digits.

              3. You want replace digits that appear in tables only.

              4. Is any other digits appear in tables.

              5. In which column your digits appear.

               

              If you give details of above points then it will easy to catch your digits and replace with your desire result.

               

              Shonky

              • 4. Re: CS4 script for converting metric values to US, and adding in a table?
                Mac_06 Level 2

                Try to Use this code,

                 

                var myDoc =  app.activeDocument;

                 

                app.findGrepPreferences = app.changeGrepPreferences = null;

                 

                app.findGrepPreferences.findWhat = '\\d+';

                 

                app.findGrepPreferences.appliedParagraphStyle = 'Your Para Style';

                app.findGrepPreferences.appliedCharacterStyle = 'Your Char Style';

                 

                var myFound = myDoc.findGrep();

                 

                app.findGrepPreferences = app.changeGrepPreferences = null;


                for (a=myFound.length-1; a>=0; a--)

                {

                myFound[a].pointSize = 10 ;  //This will become 10 pt size of your change text

                 

                 

                var myChange = myFound[a].contents + " (" + (parseInt(myFound[a].contents)/25.4)+ ")"
                myFound[a].contents = myChange.slice (0, 8) + ")"

                }

                 

                Mac

                • 5. Re: CS4 script for converting metric values to US, and adding in a table?
                  wamboyil Level 1

                  Thank you so much for your help. I have been working with this script and it's ALMOST working, here's how I have it so far:

                  var myDoc =  app.activeDocument; app.findGrepPreferences = app.changeGrepPreferences = null; app.findGrepPreferences.findWhat = '
                  d+'; app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';app.findGrepPreferences.appliedCharacterStyle = 'Dimensions'; var myFound = myDoc.findGrep(); app.findGrepPreferences = app.changeGrepPreferences = null;

                  for (a=myFound.length-1; a>=0; a--){myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text  var myChange = myFound[a].contents + " (" + (parseInt(myFound[a].contents)/25.4)+ ")"myFound[a].contents = myChange.slice (0, 8) + ")"}

                  I adjusted the point size and added in my ParagraphStyle and CharacterStyle. I'm still having one small problem with the conversion. When the conversions are being made, I need to have the result displayed to two decimal points, for example, "270" should become "270 (10.63)". However, when I run the script, it displays as "270 (10.)", with nothing after the decimal point. Also, "180" should convert to "180 (7.09)" but it displays as "180 (7.0)". It appears that maybe the result is only showing two digits total, when I would like it to display as many total digits neccesary, but with two digits AFTER the decimal point. I hope this makes sense, and would appreciate another nudge in the right direction. Again, thanks for all your help so far on this!

                  • 6. Re: CS4 script for converting metric values to US, and adding in a table?
                    wamboyil Level 1

                    Sorry, line spacing in my last post was bad...

                     

                    Thank you so much for your help. I have been working with this script and it's ALMOST working, here's how I have it so far:

                     

                    var myDoc =  app.activeDocument;


                    app.findGrepPreferences = app.changeGrepPreferences = null;


                    app.findGrepPreferences.findWhat = 'd+';


                    app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';

                    app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';


                    var myFound = myDoc.findGrep();


                    app.findGrepPreferences = app.changeGrepPreferences = null;


                    for (a=myFound.length-1; a>=0; a--)

                    {

                    myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text 


                    var myChange = myFound[a].contents + " (" + (parseInt(myFound[a].contents)/25.4)+ ")"

                    myFound[a].contents = myChange.slice (0, 8) + ")"

                    }

                     

                    I adjusted the point size and added in my ParagraphStyle and CharacterStyle. I'm still having one small problem with the conversion. When the conversions are being made, I need to have the result displayed to two decimal points, for example, "270" should become "270 (10.63)". However, when I run the script, it displays as "270 (10.)", with nothing after the decimal point. Also, "180" should convert to "180 (7.09)" but it displays as "180 (7.0)". It appears that maybe the result is only showing two digits total, when I would like it to display as many total digits neccesary, but with two digits AFTER the decimal point. I hope this makes sense, and would appreciate another nudge in the right direction. Again, thanks for all your help so far on this!

                    • 7. Re: CS4 script for converting metric values to US, and adding in a table?
                      Muppet Mark Level 5

                      If you want to round to decimal places use Math object e.g.

                       

                      var x = 270 / 25.4

                      $.writeln(x);

                       

                      var z = roundToDP(x, 2)

                      $.writeln(z);

                       

                      function roundToDP(number, decPlaces) {

                      // Decimal = base 10

                      dpNumber = Math.round(number * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);

                      return dpNumber;

                      }

                      • 8. Re: CS4 script for converting metric values to US, and adding in a table?
                        [Jongware] Most Valuable Participant

                        Mark, that does not return any trailing zeroes. Try this: multiply by the required number of tens to get rid of the actual decimal point; then round off to get rid of anything beyond that. Convert to string; sneak in a decimal point at the correct place.

                         

                         

                        val = prompt ("Any value. Any one!", );
                        var z = roundToDP(Number(val), 2)
                        alert (z);
                        
                        function roundToDP (val,dig)
                        {
                             // Including all its zeros, lopping off beyond:
                             val = Math.round(val*Math.pow(10,dig));
                             // Force in decimal point at the right place:
                             val = String(val);
                           //  return val.slice(0,val.length-dig)+"."+val.slice(dig);
                             return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
                        }
                        

                         

                        [Ed.] Oops -- didn't work for "5" to "5.00". Now it does

                        • 9. Re: CS4 script for converting metric values to US, and adding in a table?
                          Muppet Mark Level 5

                          Well spotted Jongware didn't see that… This in AppleScript always made me

                           

                          set x to round 270 / 25.4 rounding as taught in school

                          • 10. Re: CS4 script for converting metric values to US, and adding in a table?
                            wamboyil Level 1

                            I really do appreciate all the help being given! However, I have to hang my head in shame and admit again to being a scripting newbie. In the last few comments regarding the decimal point rounding, where would I insert those lines into my current script (In comment #6 above)? I'm thinking that this will be the final piece to my puzzle, and will make my life a lot happier! Thanks again!!!

                            • 11. Re: CS4 script for converting metric values to US, and adding in a table?
                              [Jongware] Most Valuable Participant

                              At a (very!) fast glance, you only have to replace this

                               

                              var myChange = myFound[a].contents + " (" + (parseInt(myFound[a].contents)/25.4)+ ")"

                              myFound[a].contents = myChange.slice (0, 8) + ")"

                               

                              by this

                               

                              myChange = myFound[a].contents+" ("+roundToDP( parseInt(myFound[a].contents), 2)+")";
                              myFound[a].contents = myChange;
                              

                               

                              -- and don't forget to include the roundToDP function at the very bottom of your script. JS is smart enough to see it's a function, so it won't try and execute it when encountered after your loop.

                               

                              (What's up with the 'slice' in your 2nd line? Was it an attempt to cut off unwanted stuff? If so, it's not necessary anymore )

                              • 12. Re: CS4 script for converting metric values to US, and adding in a table?
                                Shonkyin Level 3

                                Here is final version:

                                 

                                 

                                var myDoc =  app.activeDocument;

                                app.findGrepPreferences = app.changeGrepPreferences = null;

                                app.findGrepPreferences.findWhat = '\\d+';

                                app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';

                                app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';

                                var myFound = myDoc.findGrep();

                                 

                                app.findGrepPreferences = app.changeGrepPreferences = null;

                                 

                                for (a=myFound.length-1; a>=0; a--)

                                {

                                myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text

                                 

                                var myChange = myFound[a].contents + " (" + roundToDP ((parseInt(myFound[a].contents)/25.4),2)+ ")"

                                myFound[a].contents = myChange

                                }

                                function roundToDP (val,dig)

                                {

                                     // Including all its zeros, lopping off beyond:

                                     val = Math.round(val*Math.pow(10,dig));

                                     // Force in decimal point at the right place:

                                     val = String(val);

                                   //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                     return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                }

                                 

                                 

                                Shonky

                                • 13. Re: CS4 script for converting metric values to US, and adding in a table?
                                  wamboyil Level 1

                                  Thanks once again for all the help, I tried the latest version provided by Shonkyin, but now when I select a value and run the script on it, nothing happens. I get no error message, and nothing is added or changed at all. Also, now it appears that the client only wants the results to be shown rounded to 1 decimal point...what needs to change in the script in order to make this adjustment?

                                   

                                  Thanks thanks thanks thanks!

                                  • 14. Re: CS4 script for converting metric values to US, and adding in a table?
                                    wamboyil Level 1

                                    Does anyone have any suggestions for this issue yet? Much thanks in advance to anyone who can help me!

                                    • 15. Re: CS4 script for converting metric values to US, and adding in a table?
                                      Shonkyin Level 3

                                      Hi Dear,

                                       

                                      Above posted script working very well and if its not working at your end I am sure that there is some problem with applied paragraph and character style name. Please check it carefully.

                                       

                                      And i have change decimal to 1 in below script.

                                       

                                      var myDoc =  app.activeDocument;

                                      var result = 0;

                                      app.findGrepPreferences = app.changeGrepPreferences = null;

                                      app.findGrepPreferences.findWhat = '\\d+';

                                      app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';

                                      app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';

                                      var myFound = myDoc.findGrep();

                                      app.findGrepPreferences = app.changeGrepPreferences = null;

                                      for (a=myFound.length-1; a>=0; a--)

                                      {

                                      myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text

                                      var myChange = myFound[a].contents + " (" +  roundToDP ((parseInt(myFound[a].contents)/25.4),1) + ")"

                                      myFound[a].contents = myChange

                                      result ++

                                      }

                                      function roundToDP (val,dig)

                                      {

                                           // Including all its zeros, lopping off beyond:

                                           val = Math.round(val*Math.pow(10,dig));

                                           // Force in decimal point at the right place:

                                           val = String(val);

                                         //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                           return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                      }

                                       

                                       

                                      alert (result + " metric converted... :)")

                                       

                                       

                                       

                                      Shonky

                                      • 16. Re: CS4 script for converting metric values to US, and adding in a table?
                                        wamboyil Level 1

                                        Thanks so much, this works perfectly now! (I did have a little glitch with the character style naming.) Thanks again to everyone for their help.

                                        • 17. Re: CS4 script for converting metric values to US, and adding in a table?
                                          wamboyil Level 1

                                          Well, here I am again with another SMALL (hopefully) request. Now that I've got the script working perfectly (see above), I need to make a similar script, exactly the same as above, but instead of the mm to in conversion (divide by 25.4) I need to convert kg to lbs (multiply by 2.2). All the same criteria as before (result displays next to the original value, in parentheses, rounded to one decimal point). I thought I could figure this out myself, and in the script above, I replaced "/25.4" with "*2.2", but I was getting strange results, the rounded decimal pojnt wasn't correct in most cases, and in cases where the original value had a decimal point in it (it will only ever be one decimal point, if there is one), the result was really messed up. I thought the simple replacement of the functions would work, but I guess not.    Help, one more time, please?

                                          • 18. Re: CS4 script for converting metric values to US, and adding in a table?
                                            Shonkyin Level 3

                                            Above script showing wrong value if you have decimal in your digits because above grep search only for digits. I have corrected in below script. You getting last decimal wrong because you multiply with 2.2 this is not exact value. see in below script correct one.

                                             

                                            var myDoc =  app.activeDocument;

                                            var result = 0;

                                            app.findGrepPreferences = app.changeGrepPreferences = null;

                                            app.findGrepPreferences.findWhat = '[\\d\.]+';

                                            app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';

                                            app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';

                                            var myFound = myDoc.findGrep();

                                            app.findGrepPreferences = app.changeGrepPreferences = null;

                                            for (a=myFound.length-1; a>=0; a--)

                                            {

                                            myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text

                                            var myChange = myFound[a].contents + " (" +  roundToDP ((parseInt(myFound[a].contents)*2.20462262),1) + ")"

                                            myFound[a].contents = myChange

                                            result ++

                                            }

                                            function roundToDP (val,dig)

                                            {

                                                 // Including all its zeros, lopping off beyond:

                                                 val = Math.round(val*Math.pow(10,dig));

                                                 // Force in decimal point at the right place:

                                                 val = String(val);

                                               //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                 return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                            }

                                             

                                            alert (result + " lbs converted... :)")

                                             

                                             

                                             

                                            Shonky

                                            • 19. Re: CS4 script for converting metric values to US, and adding in a table?
                                              wamboyil Level 1

                                              Thanks once again for responding so quickly. When I use this script, it only works correctly when the original value is a whole number (with no decimals in it). However, when the original value has a decimal in it, the converted value is incorrect (according to a manual conversion  program I have been using). For example, a value of 74 returns a converted value of 163.1 (which is correct), but a value of 74.5 also returns a value of 163.1, when it should actually be 164.2. Could it be that any digit after the original decimal point is being ignored in the conversion?

                                              • 20. Re: CS4 script for converting metric values to US, and adding in a table?
                                                [Jongware] Most Valuable Participant

                                                Even before that. Shonky thoughtfully changed the FindGrep expression to search for a decimal separator, but overlooked that

                                                 

                                                parseInt(myFound[a].contents) [...]

                                                 

                                                does what the name says: make an integer of whatever is put in there. (In case you are wondering: an integer, in computer terms, is always a whole number -- no decimals allowed. For most programming languages, there is also a limit in allowed numbers -- only the biggest number that "fits" in the command processor in one go. I wouldn't know how that works for Javascript, though. End of side note.)

                                                 

                                                So instead of parseInt, you would need another function to force Javascript to treat the found text contents (which is a String) as a number. You can try this:

                                                 

                                                Number(myFound[a].contents)

                                                 

                                                but be warned, this one will fail on anything that cannot be converted to a real number. You might expect it will not, because the GREP search -- which feeds it with its results -- only searches for digits and a decimal period. Well, no. As it is, this "number" 12.34.56.78 will be found in its entirety, fed into the Number function, and it'll fail. Best is to change the GREP string to something that avoids the issue entirely, something like

                                                 

                                                app.findGrepPreferences.findWhat = '\\d*(\\.\\d+)?';

                                                 

                                                -- which will search for zero or more digits, then optionally followed by a period and one or more digits. I think this will work for anything from .0001 to 1 to 11111.

                                                • 21. Re: CS4 script for converting metric values to US, and adding in a table?
                                                  wamboyil Level 1

                                                  I tried adding the two lines suggested by Jongware, but I'm still getting the same results (correct result when the original value is a whole number, and incorrect result when the original value has a decimal point in it). Here is what the script looks like currently:

                                                   

                                                  var myDoc =  app.activeDocument;

                                                  var result = 0;

                                                  app.findGrepPreferences = app.changeGrepPreferences = null;

                                                  app.findGrepPreferences.findWhat = '\\d*(\\.\\d+)?'

                                                  app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';

                                                  app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';

                                                  var myFound = myDoc.findGrep();

                                                  app.findGrepPreferences = app.changeGrepPreferences = null;

                                                  for (a=myFound.length-1; a>=0; a--)

                                                  {

                                                  Number(myFound[a].contents)

                                                  myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text

                                                  var myChange = myFound[a].contents + " (" +  roundToDP ((parseInt(myFound[a].contents)*2.20462262),1) + ")"

                                                  myFound[a].contents = myChange

                                                  result ++

                                                  }

                                                  function roundToDP (val,dig)

                                                  {

                                                       // Including all its zeros, lopping off beyond:

                                                       val = Math.round(val*Math.pow(10,dig));

                                                       // Force in decimal point at the right place:

                                                       val = String(val);

                                                     //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                       return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                  }

                                                  • 22. Re: CS4 script for converting metric values to US, and adding in a table?
                                                    Shonkyin Level 3

                                                    Check this one but if you have decimal like 5.7.6 then it will give you wrong result and i hoping that you have not decimal like this.

                                                     

                                                    var myDoc =  app.activeDocument;

                                                    var result = 0;

                                                    app.findGrepPreferences = app.changeGrepPreferences = null;

                                                    app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+'

                                                    app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';

                                                    app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';

                                                    var myFound = myDoc.findGrep();

                                                    app.findGrepPreferences = app.changeGrepPreferences = null;

                                                    for (a=myFound.length-1; a>=0; a--)

                                                    {

                                                    myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text

                                                    var myChange = myFound[a].contents + " (" +  roundToDP ((Number(myFound[a].contents)*2.20462262),1) + ")"

                                                    myFound[a].contents = myChange

                                                    result ++

                                                    }

                                                    function roundToDP (val,dig)

                                                    {

                                                         // Including all its zeros, lopping off beyond:

                                                         val = Math.round(val*Math.pow(10,dig));

                                                         // Force in decimal point at the right place:

                                                         val = String(val);

                                                       //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                         return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                    }

                                                     

                                                     

                                                    Note: above grep not work in all case. So please be careful when using it.

                                                    • 23. Re: CS4 script for converting metric values to US, and adding in a table?
                                                      wamboyil Level 1

                                                      Thank you so much, this works perfectly now!

                                                      • 24. Re: CS4 script for converting metric values to US, and adding in a table?
                                                        wamboyil Level 1

                                                        Hello to everyone on the boards once again. I was assisted by the helpful people here a few months ago with a scripting question, and I have another question. The last version of the script I am working with is shown in post #22 above. I am converting tables full of metric dimensional values into US dimensions, in this case kg to pounds. In the latest version of the script I am using (post #22), I am working with kg values that have one decimal place in them and I am converting them into lbs values that have one decimal place in them.

                                                         

                                                        My current need is to have this script modified to be able to work with kg values that have TWO decimal places in them and convert them into lbs values that still only have ONE decimal place in them. (Ideally I would like the same script to work on kg values that are zero, one OR two decimal places, and convert any of them to lbs with one decimal place.) I tried the current script on values with two decimal places in them, and it was not working. Can someone take a look at the script in post #22 and let me know how I need to modify it to be able to work with zero, one or two decimal places.

                                                         

                                                        Thanks again so much!

                                                        • 25. Re: CS4 script for converting metric values to US, and adding in a table?
                                                          John Hawkinson Level 5

                                                          It looks to me like it should just work in that case. How does it fail?

                                                          Can you post a screenshot of the desired input data so we can make sure we understand properly?

                                                           

                                                          I do think that when Shonky wrote:

                                                           

                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+'

                                                           

                                                          that perhaps he menat to write:

                                                           

                                                          app.findGrepPreferences.findWhat = '\\<\\d+\\.\\d+|\\<\\d+';

                                                           

                                                          That is, with a double backslash before the period, and also a semicolon at the end of the line. But maybe I'm missing a subtlety. I'd think the original regexp ought to still work...

                                                          • 26. Re: CS4 script for converting metric values to US, and adding in a table?
                                                            wamboyil Level 1

                                                            Thanks for your reply. I don't know what happened but when I tried it again this morning, the script seemed to work properly as is, I didn't even need to make your suggested change. I'll keep a careful watch to make sure it continues to work properly through all of my conversions. Thanks again for your help!

                                                            • 27. Re: CS4 script for converting metric values to US, and adding in a table?
                                                              wamboyil Level 1

                                                              Hello again to the board-

                                                               

                                                              I'm resurrecting this thread to once again ask for help from the scripting gurus. Some time back, I received help in generating a Script for inDesign that would let me take a column of millimeter (mm) values in a table and convert them to inches, and display the results as such:

                                                               

                                                              10

                                                              20

                                                              45

                                                              15

                                                              10

                                                              90

                                                              55

                                                               

                                                              becomes:

                                                               

                                                              10 (.39)

                                                              20 (.79)

                                                              45 (1.77)

                                                              15 (.59)

                                                              10 (.39)

                                                              90 (3.54)

                                                              55 (2.17)

                                                               

                                                              As you can see, the mm value was converted to inches, and then the resulting inches values was placed after the mm value in parenthesis. Here is the script that members of this board helped me with:

                                                               

                                                              var myDoc =  app.activeDocument;

                                                              var result = 0;

                                                              app.findGrepPreferences = app.changeGrepPreferences = null;

                                                              app.findGrepPreferences.findWhat = '\\d+';

                                                              app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';

                                                              app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';

                                                              var myFound = myDoc.findGrep();

                                                              app.findGrepPreferences = app.changeGrepPreferences = null;

                                                              for (a=myFound.length-1; a>=0; a--)

                                                              {

                                                              myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text

                                                              var myChange = myFound[a].contents + " (" +  roundToDP ((parseInt(myFound[a].contents)/25.4),1) + ")"

                                                              myFound[a].contents = myChange

                                                              result ++

                                                              }

                                                              function roundToDP (val,dig)

                                                              {

                                                                   // Including all its zeros, lopping off beyond:

                                                                   val = Math.round(val*Math.pow(10,dig));

                                                                   // Force in decimal point at the right place:

                                                                   val = String(val);

                                                                 //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                   return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                              }

                                                               

                                                              Here is my question for today: I need to modify this script slightly so the results are displayed differently. There will still be a column of mm values that need to be converted to inches, but now I need the original mm values to be in parenthesis, after the newly added inch values, and also, if the converted inch value is less than 1, I need to add a 0 before the value (retaining the two-place decimal value) like this:

                                                               

                                                              10

                                                              20

                                                              45

                                                              15

                                                              10

                                                              90

                                                              55

                                                               

                                                              becomes:

                                                               

                                                              0.39 (10)

                                                              0.79 (20)

                                                              1.77 (45)

                                                              0.59 (15)

                                                              0.39 (10)

                                                              3.54 (90)

                                                              2.17 (55)

                                                               

                                                              I hope this makes sense. If any scripting wizards can help me with this, it woudl be much appreciated. Thanks!

                                                              • 28. Re: CS4 script for converting metric values to US, and adding in a table?
                                                                [Jongware] Most Valuable Participant

                                                                Still a piece of cake. Note the minor changes: '2' in the call to roundToDP to get two digits (the previous version had only 1); re-ordering of the myChange string; last line of the roundToDP function adjusted to add a '0' if the result length is "less than the number of digits". Only figuring out that last one took some lateral thinking

                                                                 

                                                                var myDoc =  app.activeDocument;
                                                                var result = 0;
                                                                app.findGrepPreferences = app.changeGrepPreferences = null;
                                                                app.findGrepPreferences.findWhat = '\\d+';
                                                                app.findGrepPreferences.appliedParagraphStyle = 'TablasTexto';
                                                                app.findGrepPreferences.appliedCharacterStyle = 'Dimensions';
                                                                var myFound = myDoc.findGrep();
                                                                app.findGrepPreferences = app.changeGrepPreferences = null;
                                                                for (a=myFound.length-1; a>=0; a--)
                                                                {
                                                                  myFound[a].pointSize = 7 ;  //This will become 7 pt size of your change text
                                                                  var myChange = roundToDP ((parseInt(myFound[a].contents)/25.4),2) + " ("+myFound[a].contents + ")";
                                                                  myFound[a].contents = myChange;
                                                                  result++;
                                                                }
                                                                
                                                                function roundToDP (val,dig)
                                                                {
                                                                  // Including all its zeros, lopping off beyond:
                                                                  val = Math.round(val*Math.pow(10,dig));
                                                                  // Force in decimal point at the right place:
                                                                  val = String(val);
                                                                  //  return val.slice(0,val.length-dig)+"."+val.slice(dig);
                                                                  //  return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
                                                                  return (val.length <= dig ? '0' : '') + val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);
                                                                }
                                                                
                                                                • 29. Re: CS4 script for converting metric values to US, and adding in a table?
                                                                  wamboyil Level 1

                                                                  Thank you for the help. However, when I tried this script, I got strange results when the original mm value had a decimal point in it. Here's an example:

                                                                   

                                                                  Original:

                                                                  57.8

                                                                  47.5

                                                                  6.2

                                                                  4

                                                                   

                                                                  After running script:

                                                                  2.24 (57).0.31 (8)

                                                                  1.85 (47).0.20 (5)

                                                                  0.24 (6).0.8 (2)

                                                                  0.16 (4)

                                                                   

                                                                  The last value in the example (4) is exactly what I'm looking for. But it looks like the other values are being split apart at the decimal point and each part is converted separately. Any sugegstions? Thanks again!

                                                                  • 30. Re: CS4 script for converting metric values to US, and adding in a table?
                                                                    [Jongware] Most Valuable Participant

                                                                    Hmmm ... none of your example values had a decimal point in them. Can't you apply the same solution as to one of your earlier scripts? The one that also deals with decimals?

                                                                    • 31. Re: CS4 script for converting metric values to US, and adding in a table?
                                                                      briank83163125

                                                                      thanks for the forum.  I just applied this script to a document where I needed to convert many different types of european standard measurements to u.s.standards.  All I had to do was change the math equations and create specific cell styles for each different conversion.  For a while I kept getting a string error referring to my script line 5 of above because I had my style in a style group folder and the script couldn't find it.  Note to anyone using scripts referring to styles: The style cannot be under a style group folder for this script to work.  I'm assuming there is a way to reference the style group folder in the script but for this script, if you have your style in a folder; drag it out of the folder.

                                                                       

                                                                      One Question,  my numbers are coming from europe and instead of a point . they use a comma , for decimals eg. 10.2 is 10,2.  The conversion still works if there is a comma. if you change it to: app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+'; but the returned number in the ( ) still has a comma.  What is a script way to change it to a point .  I know I can do a find/change in indesign.

                                                                      • 32. Re: CS4 script for converting metric values to US, and adding in a table?
                                                                        [Jongware] Most Valuable Participant

                                                                        A blast from the past! The problem seems to be that "1,5" gets found but cannot be converted to a valid floating point number, which indeed needs a full stop. Assuming the find step works correctly, you can try this inside your current line that does the calculation:

                                                                         

                                                                        Number(myFound[a].contents.replace(',', '.'))

                                                                         

                                                                         

                                                                        Note the "replace" added inside the Number function, which will act on "myFound[a].contents" (which, in turn, is a JavaScript representation of the text in InDesign).

                                                                        • 33. Re: CS4 script for converting metric values to US, and adding in a table?
                                                                          briank83163125 Level 1

                                                                          Thanks Jongware (as you can see your knowledge is helpful and relevant for years).  I am sorry.  I was wrong in thinking the script was working with the comma.  It was in fact not.  The comma was causing the script to calculate two separate equations but I had removed parts of the code that made me think it was working because it was only returning the calculation before the comma.  I have my script working now so that it calculates correctly but I have to find/replace all the commas with points before running it.

                                                                           

                                                                          I would like in the future to add to the script to find/replace the comma within each specified character style so I do not have to do it manually before hand.  Any help with that would be appreciated.  I'll paste my working script with multiple conversions.  I added to the bottom of the script, a section to change the character style after converting so that I would not convert the same number twice, in case i wanted to add conversions later.  hope this helps someone else.

                                                                           

                                                                          // M to ft:

                                                                           

                                                                           

                                                                          var myDoc =  app.activeDocument;

                                                                          var result = 0;

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+';

                                                                          app.findGrepPreferences.appliedCharacterStyle = 'M';

                                                                          var myFound = myDoc.findGrep();

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          for (a=myFound.length-1; a>=0; a--)

                                                                          {

                                                                          myFound[a].pointSize =10;  //This will become 7 pt size of your change text

                                                                          var myChange = roundToDP ((Number(myFound[a].contents)*3.28084),2)

                                                                          myFound[a].contents = myChange

                                                                          result ++

                                                                          }

                                                                          function roundToDP (val,dig)

                                                                          {

                                                                               // Including all its zeros, lopping off beyond:

                                                                               val = Math.round(val*Math.pow(10,dig));

                                                                               // Force in decimal point at the right place:

                                                                               val = String(val);

                                                                             //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                               return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                                          }

                                                                           

                                                                           

                                                                          // mm to inches:

                                                                           

                                                                           

                                                                          var myDoc =  app.activeDocument;

                                                                          var result = 0;

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+';

                                                                          app.findGrepPreferences.appliedCharacterStyle = 'mm';

                                                                          var myFound = myDoc.findGrep();

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          for (a=myFound.length-1; a>=0; a--)

                                                                          {

                                                                          myFound[a].pointSize =10;  //This will become 10 pt size of your change text

                                                                          var myChange = roundToDP ((Number(myFound[a].contents)*0.0393701),2)

                                                                          myFound[a].contents = myChange

                                                                          result ++

                                                                          }

                                                                          function roundToDP (val,dig)

                                                                          {

                                                                               // Including all its zeros, lopping off beyond:

                                                                               val = Math.round(val*Math.pow(10,dig));

                                                                               // Force in decimal point at the right place:

                                                                               val = String(val);

                                                                             //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                               return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                                          }

                                                                           

                                                                           

                                                                          // kg to lbs:

                                                                           

                                                                           

                                                                          var myDoc =  app.activeDocument;

                                                                          var result = 0;

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+';

                                                                          app.findGrepPreferences.appliedCharacterStyle = 'kg';

                                                                          var myFound = myDoc.findGrep();

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          for (a=myFound.length-1; a>=0; a--)

                                                                          {

                                                                          myFound[a].pointSize =10;  //This will become 10 pt size of your change text

                                                                          var myChange = roundToDP ((Number(myFound[a].contents)*2.20462),2)

                                                                          myFound[a].contents = myChange

                                                                          result ++

                                                                          }

                                                                          function roundToDP (val,dig)

                                                                          {

                                                                               // Including all its zeros, lopping off beyond:

                                                                               val = Math.round(val*Math.pow(10,dig));

                                                                               // Force in decimal point at the right place:

                                                                               val = String(val);

                                                                             //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                               return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                                          }

                                                                           

                                                                           

                                                                          // L to Gallons:

                                                                           

                                                                           

                                                                          var myDoc =  app.activeDocument;

                                                                          var result = 0;

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+';

                                                                          app.findGrepPreferences.appliedCharacterStyle = 'L';

                                                                          var myFound = myDoc.findGrep();

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          for (a=myFound.length-1; a>=0; a--)

                                                                          {

                                                                          myFound[a].pointSize =10;  //This will become 10 pt size of your change text

                                                                          var myChange = roundToDP ((Number(myFound[a].contents)*0.264172),2)

                                                                          myFound[a].contents = myChange

                                                                          result ++

                                                                          }

                                                                          function roundToDP (val,dig)

                                                                          {

                                                                               // Including all its zeros, lopping off beyond:

                                                                               val = Math.round(val*Math.pow(10,dig));

                                                                               // Force in decimal point at the right place:

                                                                               val = String(val);

                                                                             //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                               return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                                          }

                                                                           

                                                                           

                                                                          // Bar to Psi:

                                                                           

                                                                           

                                                                          var myDoc =  app.activeDocument;

                                                                          var result = 0;

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+';

                                                                          app.findGrepPreferences.appliedCharacterStyle = 'Bar';

                                                                          var myFound = myDoc.findGrep();

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          for (a=myFound.length-1; a>=0; a--)

                                                                          {

                                                                          myFound[a].pointSize =10;  //This will become 10 pt size of your change text

                                                                          var myChange = roundToDP ((Number(myFound[a].contents)*14.5038),2)

                                                                          myFound[a].contents = myChange

                                                                          result ++

                                                                          }

                                                                          function roundToDP (val,dig)

                                                                          {

                                                                               // Including all its zeros, lopping off beyond:

                                                                               val = Math.round(val*Math.pow(10,dig));

                                                                               // Force in decimal point at the right place:

                                                                               val = String(val);

                                                                             //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                               return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                                          }

                                                                           

                                                                           

                                                                          // kn to Lbf:

                                                                           

                                                                           

                                                                          var myDoc =  app.activeDocument;

                                                                          var result = 0;

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+';

                                                                          app.findGrepPreferences.appliedCharacterStyle = 'kn';

                                                                          var myFound = myDoc.findGrep();

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          for (a=myFound.length-1; a>=0; a--)

                                                                          {

                                                                          myFound[a].pointSize =10;  //This will become 10 pt size of your change text

                                                                          var myChange = roundToDP ((Number(myFound[a].contents)*0.224809),2)

                                                                          myFound[a].contents = myChange

                                                                          result ++

                                                                          }

                                                                          function roundToDP (val,dig)

                                                                          {

                                                                               // Including all its zeros, lopping off beyond:

                                                                               val = Math.round(val*Math.pow(10,dig));

                                                                               // Force in decimal point at the right place:

                                                                               val = String(val);

                                                                             //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                               return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                                          }

                                                                           

                                                                           

                                                                          // tm to ft-T:

                                                                           

                                                                           

                                                                          var myDoc =  app.activeDocument;

                                                                          var result = 0;

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+';

                                                                          app.findGrepPreferences.appliedCharacterStyle = 'tm';

                                                                          var myFound = myDoc.findGrep();

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          for (a=myFound.length-1; a>=0; a--)

                                                                          {

                                                                          myFound[a].pointSize =10;  //This will become 10 pt size of your change text

                                                                          var myChange = roundToDP (((Number(myFound[a].contents)*6561.67976752)/2000),2)

                                                                          myFound[a].contents = myChange

                                                                          result ++

                                                                          }

                                                                          function roundToDP (val,dig)

                                                                          {

                                                                               // Including all its zeros, lopping off beyond:

                                                                               val = Math.round(val*Math.pow(10,dig));

                                                                               // Force in decimal point at the right place:

                                                                               val = String(val);

                                                                             //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                               return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                                          }

                                                                           

                                                                           

                                                                          // kNm to lbft:

                                                                           

                                                                           

                                                                          var myDoc =  app.activeDocument;

                                                                          var result = 0;

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          app.findGrepPreferences.findWhat = '\\<\\d+.\\d+|\\<\\d+';

                                                                          app.findGrepPreferences.appliedCharacterStyle = 'kNm';

                                                                          var myFound = myDoc.findGrep();

                                                                          app.findGrepPreferences = app.changeGrepPreferences = null;

                                                                          for (a=myFound.length-1; a>=0; a--)

                                                                          {

                                                                          myFound[a].pointSize =10;  //This will become 10 pt size of your change text

                                                                          var myChange = roundToDP ((Number(myFound[a].contents)*737.5621211697),2)

                                                                          myFound[a].contents = myChange

                                                                          result ++

                                                                          }

                                                                          function roundToDP (val,dig)

                                                                          {

                                                                               // Including all its zeros, lopping off beyond:

                                                                               val = Math.round(val*Math.pow(10,dig));

                                                                               // Force in decimal point at the right place:

                                                                               val = String(val);

                                                                             //  return val.slice(0,val.length-dig)+"."+val.slice(dig);

                                                                               return val.slice(0,val.length-dig)+"."+val.slice(val.length-dig);

                                                                          }

                                                                           

                                                                           

                                                                          Character1(); 

                                                                           

                                                                          function Character1() { 

                                                                              var foundItem;     

                                                                              var doc = app.activeDocument; 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                               

                                                                              app.findGrepPreferences.findWhat = ".+"; 

                                                                              app.findGrepPreferences.appliedCharacterStyle = "kNm";

                                                                                    

                                                                              var foundItems = doc.findGrep(); 

                                                                               

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

                                                                                  foundItem = foundItems[i]; 

                                                                                  foundItem.applyCharacterStyle(doc.characterStyles.item("done style"), false); 

                                                                              } 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                          }

                                                                           

                                                                           

                                                                          // change style:

                                                                          //tm:

                                                                           

                                                                           

                                                                          Character2(); 

                                                                           

                                                                          function Character2() { 

                                                                              var foundItem;     

                                                                              var doc = app.activeDocument; 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                               

                                                                              app.findGrepPreferences.findWhat = ".+"; 

                                                                              app.findGrepPreferences.appliedCharacterStyle = "tm";

                                                                                    

                                                                              var foundItems = doc.findGrep(); 

                                                                               

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

                                                                                  foundItem = foundItems[i]; 

                                                                                  foundItem.applyCharacterStyle(doc.characterStyles.item("done style"), false); 

                                                                              } 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                          }

                                                                           

                                                                           

                                                                          // change style:

                                                                          //mm:

                                                                           

                                                                           

                                                                          Character3(); 

                                                                           

                                                                          function Character3() { 

                                                                              var foundItem;     

                                                                              var doc = app.activeDocument; 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                               

                                                                              app.findGrepPreferences.findWhat = ".+"; 

                                                                              app.findGrepPreferences.appliedCharacterStyle = "mm";

                                                                                    

                                                                              var foundItems = doc.findGrep(); 

                                                                               

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

                                                                                  foundItem = foundItems[i]; 

                                                                                  foundItem.applyCharacterStyle(doc.characterStyles.item("done style"), false); 

                                                                              } 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                          }

                                                                           

                                                                           

                                                                          // change style:

                                                                          //kn:

                                                                           

                                                                           

                                                                          Character4(); 

                                                                           

                                                                          function Character4() { 

                                                                              var foundItem;     

                                                                              var doc = app.activeDocument; 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                               

                                                                              app.findGrepPreferences.findWhat = ".+"; 

                                                                              app.findGrepPreferences.appliedCharacterStyle = "kn";

                                                                                    

                                                                              var foundItems = doc.findGrep(); 

                                                                               

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

                                                                                  foundItem = foundItems[i]; 

                                                                                  foundItem.applyCharacterStyle(doc.characterStyles.item("done style"), false); 

                                                                              } 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                          }

                                                                           

                                                                           

                                                                           

                                                                           

                                                                          // change style:

                                                                          //Bar:

                                                                           

                                                                           

                                                                          Character5(); 

                                                                           

                                                                          function Character5() { 

                                                                              var foundItem;     

                                                                              var doc = app.activeDocument; 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                               

                                                                              app.findGrepPreferences.findWhat = ".+"; 

                                                                              app.findGrepPreferences.appliedCharacterStyle = "Bar";

                                                                                    

                                                                              var foundItems = doc.findGrep(); 

                                                                               

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

                                                                                  foundItem = foundItems[i]; 

                                                                                  foundItem.applyCharacterStyle(doc.characterStyles.item("done style"), false); 

                                                                              } 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                          }

                                                                           

                                                                           

                                                                          // change style:

                                                                          //L:

                                                                           

                                                                           

                                                                          Character6(); 

                                                                           

                                                                          function Character6() { 

                                                                              var foundItem;     

                                                                              var doc = app.activeDocument; 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                               

                                                                              app.findGrepPreferences.findWhat = ".+"; 

                                                                              app.findGrepPreferences.appliedCharacterStyle = "L";

                                                                                    

                                                                              var foundItems = doc.findGrep(); 

                                                                               

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

                                                                                  foundItem = foundItems[i]; 

                                                                                  foundItem.applyCharacterStyle(doc.characterStyles.item("done style"), false); 

                                                                              } 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                          }

                                                                           

                                                                           

                                                                          // change style:

                                                                          //kg:

                                                                           

                                                                           

                                                                          Character7(); 

                                                                           

                                                                          function Character7() { 

                                                                              var foundItem;     

                                                                              var doc = app.activeDocument; 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                               

                                                                              app.findGrepPreferences.findWhat = ".+"; 

                                                                              app.findGrepPreferences.appliedCharacterStyle = "kg";

                                                                                    

                                                                              var foundItems = doc.findGrep(); 

                                                                               

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

                                                                                  foundItem = foundItems[i]; 

                                                                                  foundItem.applyCharacterStyle(doc.characterStyles.item("done style"), false); 

                                                                              } 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                          }

                                                                           

                                                                           

                                                                          // change style:

                                                                          //M:

                                                                           

                                                                           

                                                                          Character8(); 

                                                                           

                                                                          function Character8() { 

                                                                              var foundItem;     

                                                                              var doc = app.activeDocument; 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                               

                                                                              app.findGrepPreferences.findWhat = ".+"; 

                                                                              app.findGrepPreferences.appliedCharacterStyle = "M";

                                                                                    

                                                                              var foundItems = doc.findGrep(); 

                                                                               

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

                                                                                  foundItem = foundItems[i]; 

                                                                                  foundItem.applyCharacterStyle(doc.characterStyles.item("done style"), false); 

                                                                              } 

                                                                           

                                                                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING; 

                                                                          }