User Tools

Site Tools


manual:Calculations

Calculations

Overview

Keyboard Maestro supports calculations in almost any numeric text fields. For example you can Pause for “60*Time in Minute”s. Calculations can also use comma separated lists of numbers as arrays, and can return such arrays, so you can operate on frames and points.

Examples

FIXME [@JMichael: This needs to be replaced with screenshots of KM Actions. The “code” below is NOT supported by KM]

This is Pseudo Code Representing KM Actions

Set variable ‘Temp’ To Text ‘%WindowFrame%1%’
Set variable ‘Temp’ To Calculation ‘Temp.x+Temp.width/2,Temp.y+Temp.height/2’
Use ‘Temp’ to Set the Mouse Location

will result in the mouse being placed at the center of the front window.

Note: You must use commas for this purpose, and full stops (.) for decimal numbers, and never use any thousands separators, regardless of your desired language.

Expressions

Keyboard Maestro’s expressions include precedence, nested bracketed expressions, many built-in functions, various numeric bases, so you should be able to write almost any expressions you might like to use, as well as use it as a general purpose calculator if desired.

Operators

Operators based on precedence from lowest to highest are:

array separator (,) separates elements of an array
ternary operator (?) a=b ? 3 : 4
bitwise or (|), bitwise and (&) and bitwise xor operators
comparison operators (<, ≤, =, >, ≥, ≠) compare for (in)equality and return 0 or 1
shift operators (≪, ≫) shift a number left or right
addition operators (+, -) add or subtract
multiplication operators (*, /, MOD) multiply, divide or Modulo
power operator (^) exponentiation
unary prefix operators (√ (Option-V), -, brackets) square root, negation, sub-expressions
functions a large variety of functions
numbers and variables or array accesses (5,$5A,0x50,8#007,Variable,Variable[5]) identifiers and values
unary postfix operators (!,%,° (Option-Shift-8)) factorial, percent, degrees

To minimize conflict with variables, operators and functions must be in uppercase.

Functions

The available functions are listed on the wiki Functions page.

Screen Coordinates

Keyboard Maestro refers to screen coordinates as two or four comma separated numbers in the text of a Keyboard Maestro Variable (which is always a string).

  • Screen object points, like the left,top position of a window, with two values, like 12,34
  • Screen object rectangles, like the frame of a window, have four values 12,34,56,78 (with a fifth value for fuzz in some instances).
    * You can reference these values by position in the string, as it the Variable were an array:
  • For example:
    if the Variable myWindow is “12,34,56,78”
    then all of these forms of reference will provide a value of 34 in a Calculation field:
    • myWindow[2]
    • myWindow.Y
    • myWindow.Top

Variable Dot Notation

In a Calculation field you can reference the numbers in a Keyboard Maestro Variable (which is always a string) using dot notation:
(The Variable Name and Dot reference are case insensitive.)

Variable.x x coordinate
Variable.y y coordinate
Variable.left the left coordinate of a rectangle
Variable.top the top coordinate of a rectangle
Variable.right the right coordinate of a rectangle
Variable.bottom the right coordinate of a rectangle
Variable.width the width of a rectangle or size
Variable.height the height of a rectangle or size
Variable.fuzz the fuzz of an image match (rectangle,fuzz)
Variable.MidX the horizontal middle of a rectangle
Variable.MidY the vertical middle of a rectangle

Examples of Variable Data Reference

Macro Actions

Example Results

Some example expressions might be:

Amount in Dollars * 100
MJD() > 55928
NOW() > TIME(2012,3,23,12,2,1)
DOW(TIME(2012,4,4)) = 4
Radius*SIN(20°),Radius*COS(20°)
Window Frame[1]+Window Frame[3]/2,Window Frame.MidY
MOUSEBUTTON() + 2 * MOUSEBUTTON(4)
SCREEN(Internal,Left,10%)
manual/Calculations.txt · Last modified: 2017/03/10 20:15 by JMichaelTX