2.5. Arithmetic Operators and Expressions

AppleScript supports the basic operations of addition, subtraction, multiplication, and division. Several other operations are also supported, as you learn in the following Try It Out.

2.5.1.

2.5.1.1. Try It Out: Understanding Arithmetic Operations

Follow these steps to get some practice with arithmetic operations.

  1. Type the following program into Script Editor:

    -- Basic arithmetic operations
    
    set a to 10
    set b to 3
    set c to 25
    set d to 4
    
    log -a    -- negation
    log a + b -- addition
    log a - b -- subtraction
    log a * b -- multiplication
    log a / b -- division
    log a div b -- integer division
    log a mod b -- remainder
    log a ^ b -- exponentiation
    
    -- operator precedence
    
    log a + b * c
    log a * b + c * d
    log a * (b + c) * d -- parentheses
    log a ^ -b * c
    
    log 100 mod 10 div 3
  2. Click the Event Log tab and run the program. You get the following results in the lower pane:

    (*-10*)
    (*13*)
    (*7*)
    (*30*)
    (*3.333333333333*)
    (*3*)
    (*1*)
    (*1000.0*)
    (*85*)
    (*130*)
    (*1120*)
    (*0.025*)
    (*0*)
2.5.1.2. How It Works

The following table summarizes AppleScript's arithmetic operators.

OperatorMeaningType of Result
unary minusinteger or real
^exponentiationreal
*

/

div

mod
multiplication

division

integer division

integer remainder
integer or real

real

integer

integer or real
+ −addition subtractioninteger or real integer or real

All operators except the unary minus operator are binary operators, meaning that they operate on two values, one listed before and the other after the operator. ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.