8 Replies Latest reply on Oct 5, 2012 11:20 PM by ozbongo

    Imperial/metric conversion of text

    ozbongo

      Hi everyone,

       

      Im after a script or some direction for a script to convert a large number of text objects. I'd like to be able to convert from metres to yards and vice versa. All the text objects would be on the same layer and only contain integers and symbols such as (25) -23.

       

      If the conversion could select all the text on one layer, and then convert and copy to a new layer it would be helpful but this can be done manually if needed. Is this possible? Im not familiar with scripting really but can learn what i need to get this script going. Any advice is appreciated.

       

      Thanks

      Alex

        • 1. Re: Imperial/metric conversion of text
          Muppet Mark Level 5

          This should give you a basic example… You can do this just using plain JavaScript Math or by using unitValues… This should be easy enough for you to play with… As is… it only works with text contents that are all numbers… You will need to explain further your text I think…

           

          #target illustrator
          
          addMeasurements();
          
          function addMeasurements() {
              
              var i, doc, imp, met, txtItems, txt, nub;
          
              doc = app.activeDocument;
          
              imp = doc.layers.getByName( 'Inch' );
          
              met = doc.layers.add();  met.name = 'MM';
                    
              txtItems = imp.textFrames;
          
              for ( i = 0; i < txtItems.length; i++ ) {
          
                  txtItems[i].duplicate( met, ElementPlacement.PLACEATEND )
          
              };
          
              txtItems = met.textFrames;
          
              for ( i = 0; i < txtItems.length; i++ ) {
          
                  var nub = parseInt( txt = txtItems[i].contents ); // For Numbers only!!!!
                  
                  txtItems[i].contents = String( nub * 2.83464567 )
          
              };
          
          };
          
          • 2. Re: Imperial/metric conversion of text
            ozbongo Level 1

            Thanks for the reply and script. Sorry i should have explained it a bit more, and also say that im working on a mac. Can i use or convert this in Applescript?

             

            We have a multiple artboard project (about 50 artboards) with around 20-30 numbers on each artboard. These are meter measurements in the format that i showed initally, something like this 105 (101)

             

            What we want to do is convert these measurements to yards, without having to do it manually, which take a long time. Im a cad user and have a great script i use to do things such as this in cad, but havent found the same thing for Illustrator.

             

            Alex

            • 3. Re: Imperial/metric conversion of text
              Muppet Mark Level 5

              The above code is Abobe's ExtendScript this makes it good for both platforms… No need to convert… You can cut & paste from the forum into the ExtendScript Toolkit app that should be in your utilities folder? Just name/save it then put in AI presets folder…

              • 4. Re: Imperial/metric conversion of text
                ozbongo Level 1

                Thats great. I'll give it a go.

                 

                Alex

                • 5. Re: Imperial/metric conversion of text
                  ozbongo Level 1

                  The script is great thanks. Does pretty much what i need. Just wondering two things;

                   

                  - Can it be made to round up ro down to whole numbers?

                  - Can it be made to read numbers that are between parentheses? In a text frame such as 100 (90)  it picks up the first number, however misses the second.

                   

                  I found this script for Indesign, which i know is better at scripting than Illustrator, but looks fairly extensive.

                  http://indesignsecrets.com/free-script-to-change-all-numbers-in-a-document-using-math.php

                   

                  I really appreciate what you have already done, very helpful thanks.

                   

                  Alex

                  • 6. Re: Imperial/metric conversion of text
                    Muppet Mark Level 5

                    The math thats in it at the moment is just inch to mm… Yep you can add a rounding function easy enough… What other characters are or can be in this text? and is 100 (90) in the same line of text?

                    • 7. Re: Imperial/metric conversion of text
                      ozbongo Level 1

                      I figured out the mulitplication factor and have it set at 1.1 for metres to yards, and will make a duplicate script for yards to metres aslo.

                       

                      The lines will only ever be in about four formats, containing numbers, parentheses and - or + symbols;

                       

                      000

                       

                      000 (000)  or will be 2 digit numbers or combinatin of both

                       

                      000  -0

                       

                      000  +0    as 2 line text frame

                      (000)

                       

                      No letters or other symbols are ever used.

                       

                      Thanks

                      • 8. Re: Imperial/metric conversion of text
                        ozbongo Level 1

                        Hi Mark,

                         

                        Was wondering if you had anymore advice on this? Thanks

                         

                        Alex