12 Replies Latest reply on Jul 8, 2009 12:34 AM by seb400

    Is it possible to convert digits to text strings?

    seb400 Level 1

      I wondered if it's possible?

       

      As a design feature, I'd like to have (automatic) page numbers depicted as text.

      Is it possible to script an automatic page number into a text string. I visualise having the page number bottom left/right of pages with the text running vertically in a separate text box.

       

      1 - one

      22 - twenty two

      234 - two hundred and thirty four

       

      MTIA

      Steve

        • 1. Re: Is it possible to convert digits to text strings?
          [Jongware] Most Valuable Participant

          Absolutely, JavaScript shouldn't have any problems with that. A quick google returned this interesting page (and site!): http://www.merlyn.demon.co.uk/js-maths.htm#Cash

          • 2. Re: Is it possible to convert digits to text strings?
            [Jongware] Most Valuable Participant

            Just cobbled this together for the fun of it

             

            xpos = 10; // location of page number frame
            ypos = 20;
             var oldHorizontalUnits = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
            var oldVerticalUnits = app.activeDocument.viewPreferences.verticalMeasurementUnits;
            var oldRuler = app.activeDocument.viewPreferences.rulerOrigin;
            app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
            app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.POINTS;
            app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.POINTS;
            for (p=0; p<app.activeDocument.pages.length; p++)
            {
             frame = app.activeDocument.pages[p].textFrames.add (undefined, undefined, undefined, {absoluteRotationAngle:90} );
             if (p & 1)
              frame.geometricBounds = [xpos,ypos, xpos+96, ypos+12];
             else
              frame.geometricBounds = [xpos,app.activeDocument.documentPreferences.pageWidth-ypos, xpos+96, app.activeDocument.documentPreferences.pageWidth-ypos-12];
             frame.parentStory.appliedFont = app.fonts.item("Myriad Pro\tRegular");
             frame.parentStory.appliedFontSize = 8;
             frame.parentStory.kerningMethod = "Optical";
             frame.parentStory.justification = Justification.RIGHT_ALIGN;
             frame.contents = numToText (p+1);
            }
            app.activeDocument.viewPreferences.horizontalMeasurementUnits = oldHorizontalUnits;
            app.activeDocument.viewPreferences.verticalMeasurementUnits = oldVerticalUnits;
            app.activeDocument.viewPreferences.rulerOrigin = oldRuler;
            function numToText (a)
            {
             var numbers = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
                 "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ];
             var bigger = [ "", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" ];
             if ((a % 100) < 20)
              text = numbers[a % 20];
             else
             {
              text = bigger[Math.floor(a/10) % 10];
              if (a % 10)
              {
               if (a % 100)
                text = text+" " + numbers[a % 10];
               else
                text = numbers[a % 10];
              }
             }
             b = Math.floor(a/100);
             if (b)
             {
              if (a % 100)
              {
               if (b % 10)
                text = numbers[b % 10]+" hundred and "+text;
               else
                text = numbers[b % 10]+" and "+text;
              } else
              {
               if (b % 10)
                text = numbers[b % 10]+" hundred";
              }
              if (b > 9)
              {
               if (a % 100)
                text = " "+text;
               text = numbers[Math.floor(b/10)]+" thousand"+text;
              }
             }
             return text;
            }
            
            • 3. Re: Is it possible to convert digits to text strings?
              Thomas B. Nielsen Level 3

              Had to do this one also :-)

               

              Add a textframe on the masterpage, give it the script label pagenumberastext (window -> Automation -> Script label), run the script

               

              var myDocument = app.activeDocument;
              var cleanup = true;

               

              if (cleanup) {
                  var myCleanUpItems = new Array();
                  var myCleanedPages = new Array();
                  var cleaned = false;
                  for (p = 0; p < myDocument.pages.length; p++) {
                      for ( j = 0; j < myDocument.pages[p].pageItems.length; j++) {
                          if (myDocument.pages[p].pageItems[j].label == "pagenumberastext") {
                              myDocument.pages[p].pageItems[j].removeOverride();
                          }
                      }
                  }
              }

               

              for (p = 0; p < myDocument.pages.length; p++) {
                  var myPageNum = myDocument.pages[p].name;
                  var myMasterItems = myDocument.pages[p].masterPageItems;
                  for ( j = 0; j < myMasterItems.length; j++) {
                      if (myMasterItems[j].label == "pagenumberastext") {
                          var myFrame = myMasterItems[j].override(myDocument.pages[p]);
                          myFrame.contents =  convert(myPageNum);
                      }
                  }
              }

               

              //http://javascript.internet.com/math-related/write-out-number.html
              var n = "";
              function convert(input) {
                  n = "";
                  var inputlength = input.length;
                  var x = 0;
                  var teen1 = "";
                  var teen2 = "";
                  var teen3 = "";
                  var numName = "";
                  var invalidNum = "";
                  var a1 = ""; // for insertion of million, thousand, hundred
                  var a2 = "";
                  var a3 = "";
                  var a4 = "";
                  var a5 = "";
                  digit = new Array(inputlength); // stores output
                  for (i = 0; i < inputlength; i++)  {
                  // puts digits into array
                  digit[inputlength - i] = input.charAt(i)};
                  store = new Array(9); // store output
                  for (var i = 0; i < inputlength; i++) {
                  x= inputlength - i;
                  switch (x) { // assign text to each digit
                  case x=9: d1(digit[x]); store[x] = n; break;
                  case x=8: if (digit[x] == "1") {teen3 = "yes"}
                            else {teen3 = ""}; d2(digit[x]); store[x] = n; break;
                  case x=7: if (teen3 == "yes") {teen3 = ""; d3(digit[x])}
                            else {d1(digit[x])}; store[x] = n; break;
                  case x=6: d1(digit[x]); store[x] = n; break;
                  case x=5: if (digit[x] == "1") {teen2 = "yes"}
                            else {teen2 = ""}; d2(digit[x]); store[x] = n; break;
                  case x=4: if (teen2 == "yes") {teen2 = ""; d3(digit[x])}   
                            else {d1(digit[x])}; store[x] = n; break;
                  case x=3: d1(digit[x]); store[x] = n; break;
                  case x=2: if (digit[x] == "1") {teen1 = "yes"}
                            else {teen1 = ""}; d2(digit[x]); store[x] = n; break;
                  case x=1: if (teen1 == "yes") {teen1 = "";d3(digit[x])}    
                            else {d1(digit[x])}; store[x] = n; break;
                  }
                  if (store[x] == "Not a Number"){invalidNum = "yes"};
                  switch (inputlength){
                  case 1:   store[2] = "";
                  case 2:   store[3] = "";
                  case 3:   store[4] = "";
                  case 4:   store[5] = "";
                  case 5:   store[6] = "";
                  case 6:   store[7] = "";
                  case 7:   store[8] = "";
                  case 8:   store[9] = "";
                  }
                  if (store[9] != "") { a1 =" Hundred, "} else {a1 = ""};
                  if ((store[9] != "")||(store[8] != "")||(store[7] != ""))
                  { a2 =" Million, "} else {a2 = ""};
                  if (store[6] != "") { a3 =" Hundred "} else {a3 = ""};
                  if ((store[6] != "")||(store[5] != "")||(store[4] != ""))
                  { a4 =" Thousand, "} else {a4 = ""};
                  if (store[3] != "") { a5 =" Hundred "} else {a5 = ""};
                  }
                  // add up text, cancel if invalid input found
                  if (invalidNum == "yes"){numName = "Invalid Input"}
                  else {
                  numName =  store[9] + a1 + store[8] + store[7]
                  + a2 + store[6] + a3 + store[5] + store[4]
                  + a4 + store[3] + a5 + store[2] + store[1];
                  }
                  store[1] = ""; store[2] = ""; store[3] = "";
                  store[4] = ""; store[5] = ""; store[6] = "";
                  store[7] = ""; store[8] = ""; store[9] = "";
                  if (numName == ""){numName = "Zero"};
                  //document.myform.textver.value = numName;
                  return numName;
              }

               

              function d1(x) { // single digit terms
                  switch(x) {
                  case '0': n= ""; break;
                  case '1': n= " One "; break;
                  case '2': n= " Two "; break;
                  case '3': n= " Three "; break;
                  case '4': n= " Four "; break;
                  case '5': n= " Five "; break;
                  case '6': n= " Six "; break;
                  case '7': n= " Seven "; break;
                  case '8': n= " Eight "; break;
                  case '9': n= " Nine "; break;
                  default: n = "Not a Number";
                  }
                  return n;
              }
              function d2(x) { // 10x digit terms
                  switch(x) {
                  case '0': n= ""; break;
                  case '1': n= ""; break;
                  case '2': n= " Twenty "; break;
                  case '3': n= " Thirty "; break;
                  case '4': n= " Forty "; break;
                  case '5': n= " Fifty "; break;
                  case '6': n= " Sixty "; break;
                  case '7': n= " Seventy "; break;
                  case '8': n= " Eighty "; break;
                  case '9': n= " Ninety "; break;
                  default: n = "Not a Number";
                  }
                  return n;
              }
              function d3(x) { // teen digit terms
                  switch(x) {
                  case '0': n= " Ten "; break;
                  case '1': n= " Eleven "; break;
                  case '2': n= " Twelve "; break;
                  case '3': n= " Thirteen "; break;
                  case '4': n= " Fourteen "; break;
                  case '5': n= " Fifteen "; break;
                  case '6': n= " Sixteen "; break;
                  case '7': n= " Seventeen "; break;
                  case '8': n= " Eighteen "; break;
                  case '9': n= " Nineteen "; break;
                  default: n=  "Not a Number";
                  }
                  return n;
              }

               

              --

              Thomas B. Nielsen

              http://www.nobrainer.dk

              • 4. Re: Is it possible to convert digits to text strings?
                [Jongware] Most Valuable Participant

                Ha -- sorry. The line

                 

                frame.parentStory.appliedFontSize = 8;

                 

                should be

                 

                frame.parentStory.pointSize = 8;

                 

                to work properly with fairly modern CSes.

                • 5. Re: Is it possible to convert digits to text strings?
                  Jeremy bowmangraphics-DQuh1B Level 3

                  Both Jongware's and Thomas's scripts are beautiful. Many thanks to both of you. But I have a beginner's question: What does that single ampersand do in Jongware's script? I guess I'm still a beginner, because I've never seen that before.

                  • 6. Re: Is it possible to convert digits to text strings?
                    Harbs. Level 6

                    Hi Jeremy,

                     

                    I just learned something new...

                     

                    Here's your answer:

                     

                    https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Bitwise_Operators

                     

                    I've never used them, and I probably never will...

                     

                    Harbs

                    • 7. Re: Is it possible to convert digits to text strings?
                      Jeremy bowmangraphics-DQuh1B Level 3

                      Hi Harbs,

                       

                      Thanks very much for that! I suppose if you think you'll never use that, it's vanishingly unlikely that I'll ever need to use it either. Interesting to know though.

                       

                      All the best -- Jeremy

                      • 8. Re: Is it possible to convert digits to text strings?
                        [Jongware] Most Valuable Participant

                        Hey Harbs -- nice of you to look it up!

                         

                        I've never used them, and I probably never will...

                         

                        They come in handy for the odd little quick expression such as this one ( (p &1) fires if p is odd), but you should see my "regular" programs

                        You probably would use (p % 2) -- hurts my boolean mind... (as do every mod, divide, and multiply by powers of 2).

                        • 9. Re: Is it possible to convert digits to text strings?
                          Thomas B. Nielsen Level 3

                          I have made a modified version for danish numbers, it can be found here:

                          http://www.nobrainer.dk/sidetal-som-tekst/

                          • 10. Re: Is it possible to convert digits to text strings?
                            Harbs. Level 6

                            I guess I could get used to using anything, and it is a much more 

                            elegant way of going about it than I what I would have done, but I 

                            really have a hard time thinking in bits... I'd take regular math 

                            any day!

                             

                            Harbs

                            • 11. Re: Is it possible to convert digits to text strings?
                              seb400 Level 1

                              Thanks Jongware I can work with that.

                              It's clear enough for me to be able to tweak successfully to suit my needs!


                              Steve

                              • 12. Re: Is it possible to convert digits to text strings?
                                seb400 Level 1

                                Thanks Thomas - perfect!

                                 

                                Steve