Skip to main content
Participant
April 22, 2010
Question

Convert numbers to words

  • April 22, 2010
  • 2 replies
  • 3099 views

I am extremely new to scripting and I am trying to use scripting to automate InDesign.

I need to convert a list of numbers to words,

I can convert each number one at a time in Excel or on a website, which is tedious...

I am working on a multi-book series where the design calls for the page number to appear as both a standard folio and the folio in English words along side it. Previously I have taken advantage of pre-written scripts to increase productivity and save time, which is addicting. I figured I can thread a series of text boxes to hold the list of number/words but I am determined not to have to type it all manually.

I have been attempting to adapt a script but I am getting frustrated, can anyone give me any advice on what needs to be done? I am not sure how to begin to tell the script to function on the selected numbers or the numbers contained within a particular text frame.

I appreciate any and all help!

Thanks so much!

This topic has been closed for replies.

2 replies

Participant
May 6, 2010

Hi, you said you are extremly new to scripting but I can tell I know nothing on scripting. I need to figure out how to convert page numbers to words in a 200 pages-book for children. Can you give me a hand with this by teach me how to do this (step by step) or link me to a good tutorial on this. Thank you very much.

Jongware
Community Expert
Community Expert
May 6, 2010

Where to start, when starting from nothing ...

Basic Javascript tutorials are all over the web. They are usually targeted towards Web stuff, so you'll have to learn to recognize what parts can safely be applied to InDesign (calculations and stuff) and what's of no use (anything mentioning HTML, for example).

A good starting point is Adobe's own introduction: http://wwwimages.adobe.com/www.adobe.com/eeurope/products/indesign/scripting/pdfs/InDesignCS4_ScriptingTutorial.pdf -- it introduces the basics of writing a script, special handling of ID's environment, and shows lots of little snippets of code to use as "building blocks" of more complicated script.

If you worked your way through that front-to-back, you can start reading from the top of this thread and (hopefully) follow the example scripts and code snippets I wrote for the previous poster.

Jongware
Community Expert
Community Expert
April 22, 2010

The stringFromNumber function in my script somewhere in this post Numbered list style: "One, two, three, four...  converts any number (in a reasonable range) to a string. Since it returns a string, you can immediately insert it into any frame, using something like "yourFrame.contents = stringFromNumber (123). This one is home-brewn, there are also other ways -- a quick google shows lots of hits.

(The initCap function is just a little helper, which you may want to use or not. It's only used in the final line of stringToNumber.)

As for 'grabbing a number to convert', you can do this

app.selection[0].contents = stringFromNumber (Number(app.selection[0].contents));

to immediately convert a selected number to text. (Go ahead -- copy the functions stringToNumber and initCap and add this line, then try it.)

Grabbing a page number is a bit more complicated -- it's not a real number but a code. Therefore, you have to a bit of digging around inside ID's objects. The frame in which the number will appear, is placed onto a page -- its parent object. This parent page has an index, which is the zero-based actual page number -- regardless of any section numbering. The page also has a name, which is the text that appears in the place of a page number code. It's important to realize it is a text, rather than a number, because "AAB" and "Section 1:xliv" are perfectly good page numbers. I suppose you set up your document to have "normal" page numbers, though, so getting the page number for a selected frame would be

Number(app.selection[0].parent.name)

and to immediately insert that number as a word into the frame, all you have to do is

app.selection[0].contents = stringFromNumber (Number(app.selection[0].parent.name));

for any selected text frame on a page (which ideally should be empty, because its contents are replaced). (Go ahead -- try this as well!)

You could do this on each of your pages ...

To get an entire list of numbers-to-convert, you could set up a paragraph to include automatic numbering. Remember to remove the default period and tab -- you'll only want the numbers. Hit enter enough times to get all the numbers you need, then select all and convert the automatic numbering to text. Then select the frame and run this script:

list = app.selection[0].contents.split("\r");

result = '';

for (a=0; a<list.length; a++)

     result += stringFromNumber(Number(list))+"\r";

app.selection[0].contents = result;

(Again, it needs the functions stringFromNumber and initCap.)

There are lots of improvements that could be made. A more complicated script could, for example, automatically create a new frame on the correct position per page, set the right font, and insert the number-as-text... But I'll leave it up to your imagination

4mary2meAuthor
Participant
April 22, 2010

Wow! I am going to go play with this NOW.

Thank you so much!