Browse the docs

calculate

Evaluates text as an expression and returns the result.

Keyboard Maestro: CALCULATE

Details #

calculate(text) takes exactly one argument. The text is run as an expression, in the same language and with the same variables as the rest of the macro, and the result comes back with its own type. Empty or blank text gives 0.

Use it when the expression itself comes from somewhere else, such as a variable the user typed or text read from a file. Everything the macro can already write inline doesn't need it.

Parameters #

Takes 1 argument. {{ calculate(text) }}

Examples #

calculate("2 + 3 * 4")
→ 14
calculate(formula)
→ 11.99
Sample variableValue
formularound(price * 1.2, 2)
price9.99

Gotchas #

  • Calling it with no argument or two arguments is an error: calculate() takes 1 argument, got 2.

  • If the text isn't a valid expression the step stops with that expression's own error, such as an unknown function or variable.

  • The text can read and compute with variables and call any function, so only pass it text you trust.

  • Keyboard Maestro's version first processes tokens in the text. Here the text is an expression, so write price, not %Variable%price%.