Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

Free Script to Change All Numbers in a Document using Math

What if you had a document with 2,000 prices in it, all in US dollars, and you wanted to change them all to Euros? Taking a look at today’s exchange rate, we see that the dollar is around 1.3 Euros. It’s easy to do a find/change to replace the $ symbol with the ?. But what about all those numbers? Wouldn’t it be cool if we had a script that simply multiplied all the numbers in the document by 1.3? Too bad that’s impossible.

Until now!

Scripting pro Steve Wareham just sent me a script that lets you multiply, divide, add, or subtract the numbers in your document. It’s called NumberAdjuster, and we’re releasing it free here, as promoware. (In other words, if you like it, go hire him to do some scripting the next time you need something automated.)

Here’s how it works: You download the script, unzip it, install it, then double-click it in the Scripts panel (Window > Automation > Scripts). [See note at end of this article for a different version of this script.] You should see a dialog box like this:

You can control whether the operation is performed on the entire document or just the current selection. You can specify which math operation to perform and what to use as the operator (such as “multiply by 1.3” or “add .99”). You can constrain the operation to numbers with a prefix (such as only numbers with a $ or £, or even only apply it to negative numbers by typing a dash). And you can constrain the operation to numbers with a particular character style.

The first time you run it, it might be slow, because it has to write a little preferences file to disk (so it remembers what you did next time you run the script). After that, it’s pretty dang fast.

Don’t type in two prefix characters (such as -$ for negative dollars). It can’t handle that, and the result will require an undo. In fact, as with running any script, I strongly suggest saving your document first, in case you need to choose file > revert. (You can undo the math operations, but it undoes them one number at a time, which isn’t much fun if you’ve just changed a thousand numbers.)

Note that this script won’t change the prefix for you (changing dollars to euros, or whatever). It only adjusts the number. But again, changing the symbol is as easy as Edit > Find/Change.

I was just chatting with Anne-Marie about this, and she told me she heard of a catalog company that put a “take 15% off all the prices on this page” sticker on every page of a big catalog because it was easier and faster than going ahead and changing the prices. No longer! This script is a marvel.

[Update: Here is an updated version of the script, written by Peter Kahrel.]

David Blatner
David Blatner is the co-founder of the Creative Publishing Network, InDesign Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at Lynda.com are among the most watched InDesign training in the world. You can find more about David at 63p.com.
David Blatner

Latest posts by David Blatner (see all)

Tags
Related Articles
Comments

0 Comments on “Free Script to Change All Numbers in a Document using Math

  1. Wow, this works great. A client produced his own index and we had to add two pages to the beginning of the book. This made the changes very easy. Second time David Blatner saved my skin. Thanks!

  2. have you any idea, how it could work with CS2?
    I´m not to sophisticated nor in my english nor in scripts – but i have to get it work …

    best wishes
    Carola

  3. I got a syntax error on this line

    for (var i = 0; i < myRoundto ? my_postComma.length; i++)

    Should be
    for (var i = 0; i < myRoundto – my_postComma.length; i++)

    It was fine for making 3.5 up to 3.50, but 3.00 still came out as 3 so I have added these lines

    if (dec_index != -2)
    {
    var my_postComma = myRoundedNum.substring(dec_index +1,myRoundedNum.length);
    for (var i = 0; i < myRoundto – my_postComma.length; i++)
    myRoundedNum += '.00';
    }

    Works okay now, but I'd like the "Only accept numbers with prefix …" nox to accept a tab , as well as a currrency symbol.
    Can this be done?

    • I tried using your script but it doesn’t work for me. If i try 1,50 x 2 it placces 3 instead of 3.00. Any Tips?

  4. I install the script into CS3, but it won’t run and I desperately need this to work. What am I doing wrong?

    Thanks.

    Tara

  5. I installed your script in indesign cs4
    but your script does not work with the comma,
    thank you for finding a solution.
    Thanks.
    Hassen

  6. Can anyone help me?
    I need to modify all the prices on the pricelist of my company, the problem is the format is either 1.200 (meaning 1200 ?) or 430 (430 ?)
    I need to add 4% to all the prices.
    I tried successfully to modifiy all the numbers *.*** using the regex system but only to add or multiply for natural numbers, I can’t multiply for 1,04 with this script.

    At the same time I need to mantain all the codes, for example 45.48.86 so I’d need to add an exception for this configuration of numbers, but not for the two digits one by themselves.

    In some places there are number like 100 that need to stay the same, usually the line is “O100” or “O 100” (it means the diameter is 100mm). If anyone can help with these, as it’ll take a life to modify all the prices manually!!

    1.000 need to become 1.040
    100->104
    O100 or O 100 stays the same
    34.56.32 stays the same
    When the number have decimals it need to round up for excess…

  7. Perfect script! Worked beautifully for updating a table of contents where the page numbers shifted a page because of edits. Thank you!

  8. Hi !
    Script worked just great! Thanks!

    For me, it would be even greater to have a customizable GREP Search for specific number, if i may add a Feature Request :)

    Thanks again
    shorshe

  9. I tried a simple test:
    5 pm
    6 pm
    11:30
    11:45.

    My results with a subtraction of 1 were ?
    4 pm
    5 pm
    10:29
    10:44

    That was a rounding factor of 0. Other rounding factors gave unpredictable results. So either I can’t figure out the variables or it cant do times with minutes. I’ll keep trying, but the secret would lie in a help file because I cant make sense of the some of the variables in the script.

  10. @Chuckie: Well, it’s not set up to specifically work with times. You’d have to probably do some tricky stuff either with grep, or with the prefix, or perhaps apply different character styles to different parts (like hours vs. minutes). I mean, after all, if you say “subtract 1” how is it supposed to know if you mean 1 minute or 1 hour?!

    • I just tried running this in InDesign CC and unfortunately it only works with natural numbers. I need to multiply by 1.25, but the script automatically corrects it to 1.

      I even tried manually editing the prefs.txt file to 1.25, which correctly displays 1.25 in the script dialogue window, but as soon as it’s run, it defaults back to 1.

      Is there a work around here?

      Any help is much appreciated. This is an awesome script and exactly what I’m looking for. Provided it works.

      • …same problem…
        I need to multiply by 1.3, but it doesn’t work :-(
        Is there an updated release of it?

        Thanks

    • Alessandro: That is strange… I have no problem with this working. (I’m currently on Mac OS 10.9.1, InDesign CC 9.2…)

      Be sure to set the “Perform Operation On” buttons to “Story of selected frame” or select the text first!

      • I have had this problem for an while now, I do an work around, first I multiply with eg. 28 then i divide with 10 this gives 2.8 :-)

  11. This is a great script! I have been using for the past 3 years. This year my client wants to increase the prices in the catalog by 4.25%. When I enter an Operator (multiply) of 1.0425 ?? it changes to 1.043. Any ideas on how I can get this to stick? I have over 1000 prices to change.

  12. Is it possible to use this script, or a modification of it, on numbers within script labels? I have multiple text frames, each with it’s own script label beginning with a number. I’d like to quickly add 1 to that number for every script label. Any ideas?

  13. I have used this script in the past and found it to be excellent for price list revision. Having not used it for nearly 12 months the script no longer seems to work?
    I am using Indesign 5.5 on OS 10.7.5 and the script I am using is number_adjuster_1_2-1.jsx
    The script dialog appears but no action is taken after making selection, opening script, setting operation and hitting OK?
    Any ideas?
    Thanks,

    Ian

  14. Hello, really clever script. However, it seems to pick up numbers in styles other than the ones I’ve specified. I’m trying to correct footnotes so asked it to only select numbers in the footnote_reference style but it it ended up performing the operation on all numbers in the document. Anyone else come across that?

  15. Some (most likely all; I didn’t verify) of my prices end up with only one digit after the period (e.g. 3.5 instead of 3.50) after multiplication by 0.9. I did mention two-digit rounding. Any idea what might have gone wrong?

  16. Hello, David~
    Seems the author of this script is gone gone gone.
    I am using it with CS5.5 on an iMac with OS 10.9.5. Soon as I download it, what do I do? I clicked on the script itself, but there was no way I could see to “load” it into existing scripts in InDesign. When I hit green RUN, there was an error in Line XXX. I dunno which line. : (
    How do I use this beautiful script with my program, please?
    Thank you,
    Will

  17. the script works great but it’s not remembering the last setting. i have 1500 to edit. would be nice if it saved the last settings

  18. This script is brilliant. Just saved my life when an author deleted an endnote from a chapter with 743 endnotes in it. I changed the endnotes to numbered para style and then just used the script minus one. Worked like a dream. Thank you so much for pointing it out.

  19. Hello David, very nice work! I have a question: What can be done, if I only need to add a number to an existing number with leading zeros, for example add 1800 to 001234? Can I suppress the point ore the comma?

  20. I reached this page after searching for a script that calculate percentage. Although it seems a pretty script it doesn’t fit my needs. Anyone know or could make a script that calculate percentages based on indesign styles to run on a full document.

    For example:

    Search values on style A
    Search values on style B
    Calculate the difference percentage of both and input on field with style C

    pratical example:
    $199,90 (style A)
    $149,90 (style B)
    25% (submit on style C text box)

Leave a Reply

Your email address will not be published. Required fields are marked *