This website contains various projects I have worked on. Currently, this includes JavaScripts for Adobe applications and general production tutorials using Adobe Creative Suite applications. All projects can be accessed from the right-hand menu.

Adobe Scripts List
Updating numbers in an InDesign document using math

Script Description

This InDesign script is meant to help you quickly update numbers in an InDesign document. It works by getting the operation you want to perform on numbers from a dialog box, and the searching for numbers that match either a default or custom regular expression. There are options to only find number that have a specific paragraph style applied or follow a specific prefix character.

Looking at the Script

The core part of this script is finding text that is actually a number. To do that it uses a regular expression: [-]*[0-9][0-9,.]*\b. With that expression the findWhat function is used to get all the matches.

//Find matching numbers
var mySearchGrep = app.findGrepPreferences.findWhat = mySearchPrefix + myGrep; 
var myFoundArray = mySearchScope[i].findGrep(); 

Once matches to the regular expression have been found we can perform the operation on them. The result of the operation is stored in the variable “myResult”.

//Perform the selected operation on the found number
            if (myOperation == "+"){var myResult =  (parseFloat(myFoundNumber) + myOperator) + "";}
                else if (myOperation == "-"){var myResult =  (parseFloat(myFoundNumber) - myOperator) + "";}
                    else if (myOperation == "*"){var myResult =  (parseFloat(myFoundNumber) * myOperator) + "";}
                        else if (myOperation == "/"){var myResult =  (parseFloat(myFoundNumber) / myOperator) + "";}
                        

The script does call a few other functions to make it more useful. It provides options to round off the result, add a prefix character to the result, and search on a custom regular expression.

Finally, there is an option for counries who use a comma for a decimal point and a decimal point for a comma, i.e. “1.234,56”. Selecting this options effects the what is found and returned via the find/change funcitons. The operator field still needs to be formatted as “1,234.56” however.

back to top

Image of a notepad

Table-of-Contents

Downloads


Other Projects:

Adobe JavaScripts ↑↓

Adobe Creative Suite Tutorials ↑↓