2 Replies Latest reply on Sep 11, 2018 7:42 AM by Awitmer

    Help with Calculator Script

    Awitmer Level 1

      Hello all!


      I am writing a simple script that functions as a basic calculator via a

      keyboard shortcut. I have the basic outline of the code below, but could

      somebody please help me know how to do the actual calculation? Or is this

      not possible? The alert just simply returns the text I entered in the GUI.

      Thanks in advance!


      #target indesign


      app.scriptPreferences.userInteractionLevel =



                      var myDialog = app.dialogs.add();




                                      //Add a dialog column.




                                                      //Create a text edit field.


                                                      var myTextEditField =

      textEditboxes.add({editContents:"", minWidth:150});






                      //Display the dialog box.


                      var myResult = myDialog.show();


                      if(myResult == true){


                                      //Get the values from the dialog box



                                      var myResult = myTextEditField.editContents;


               var myFinalResult = (myResult)


               alert (myFinalResult, "InDesignCalculator.jsx")


                                      //Remove the dialog box from memory.













        • 1. Re: Help with Calculator Script
          Manan Joshi Adobe Community Professional
          //Replace the line below with the line under it
          var myResult = myTextEditField.editContents; 
          var myResult = eval(myTextEditField.editContents);

          This should get your code working and the result would be displayed for the expression that you write in the text field of your dialog. As a next step you need to take care that eval can be dangerous as it can evaluate any js code snippet passed on to it. So a better solution would be that once you get the expression you separate the operator from the operands and check that the operands are indeed numbers, once verified only then you should pass the expression to eval.


          In order to check if a variable is number or not you can use isNaN method, if it returns false then you have a number. So your calculation code would be something like

          var operand1 = 6
          var operand1 = 7
          var opt = "*"
          if(!isNaN(operand1) && !isNaN(operand2))
               alert("Result is " + eval(a + opt + b))


          Now either you could start with having 3 textfields on you dialog one each for operand1, operand2, and the operator and then use the code above, or keep the current single text field and the write a code to separate the expression into operand1, operand2, and the operator


          Hope this gets you started.

          • 2. Re: Help with Calculator Script
            Awitmer Level 1

            Thanks so much! This gives me the direction I needed.