• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help with Calculator Script

Participant ,
Sep 10, 2018 Sep 10, 2018

Copy link to clipboard

Copied

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 =

UserInteractionLevels.interactWithAll;

var myDialog = app.dialogs.add();

with(myDialog){

//Add a dialog column.

with(dialogColumns.add()){

//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

controls.

var myResult = myTextEditField.editContents;

var myFinalResult = (myResult)

alert (myFinalResult, "InDesignCalculator.jsx")

//Remove the dialog box from memory.

myDialog.destroy();

}

else{

myDialog.destroy();

}

TOPICS
Scripting

Views

510

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 10, 2018 Sep 10, 2018

//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 oper

...

Votes

Translate

Translate
Community Expert ,
Sep 10, 2018 Sep 10, 2018

Copy link to clipboard

Copied

//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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 11, 2018 Sep 11, 2018

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines