We are not going to do a lot of scripting today; rather we are going to look a little more closely at some of the nifty mathematic functions that AppleScript offers us scripters. Although this sounds boring, a good understanding of these basic concepts can make a difference in your scripts as your skill continues to increase. First, however, let’s determine how AppleScript thinks about numbers in general. This page tells us that the term Number can be used interchangeably with the terms Integer or Real in a script, but also hints at what we really need to know, which is simply that a number of class integer is a whole number (no decimals) and that a number of class real is just what it sounds like, a real number, with all the necessary digits to the right of the decimal. (See Apple’s definitions of Integers and Real Numbers Here is an example script:
set number_1 to 24
set number_2 to 43.131
set the_product to number_1 * number_2
display dialog "Real: " & (the_product as real) & return & "Integer: " & (the_product as integer)
All we did here was define two variables as numbers (24 and 43.121), then set another variable to the product of those two numbers (number_1 * number_2). In the display dialog, you see that we took the value of that variable (the_product) and coerced it to a real number, and then to an integer. Coercion is telling AppleScript to take one type of information, and force it to another type, and it is happy to do this for you anytime you want, as long as it follows the native coercion rules. (Those rules can be found here.)
You can coerce numbers to strings, strings to numbers, integers to reals, etc., etc. In my example script, we just took the number in the_product and coerced it into both number types (real & integer) to see what it would look like, displaying that the real number preserves the decimal places, and the integer number removes all the digits to the right of the decimal.
You have all the basic mathematical operators at your disposal:
[b]* (multiplication)
- (addition)
- (subtraction)
/ or ÷ (division)[/b] (You get the ÷ symbol by pressing alt and /)
You can also use the ^ character to raise a number to a power (use SHIFT and 6 to get the character), as in this script:
8 ^ 2
In conjunction with division, you have two unique operators to play with, div and mod. (Apple’s explanations for these can be found here.)
When you use div, you get the integer result of a division operation. Here is an example script:
set number_3 to 400
set number_4 to 21
display dialog "Using simple division, 400 ÷ 21 = " & (number_3 / number_4) & return & "while using the div operand, you get: " & (number_3 div number_4)
Now, before we move on, let me see if I can read your mind. You are probably thinking that div is just like doing division and then coercing to an integer, like this:
20 ÷ 3 as integer
Well, Miss or Mister Smarty-Pants, let’s check that out:
set twenty to 20
set three to 3
display dialog "Using div: " & (twenty div three) & return & "With division & coercion: " & ((twenty / three) as integer)
This displays one of the AppleScript mathematical dirty little secrets: coercing to an integer induces rounding, while using div simply chops off the decimal data. We will talk more about rounding later, so let’s cover the usage of mod.
I am not a mathematician by any means, so I have very little idea about why this is called mod, or even what it is really useful for, but I think that it at least has a coolness factor that deserves discussion. Mod is pretty much the opposite of div, in that it returns the remainder of an unclean division operation, NOT any decimal data. For instance, 6 goes into 70 11 times with a remainder of 4, right Let’s see:
set seventy to 70
set six to 6
display dialog "For the expression 70 ÷ 6:
Division: " & seventy div six & return & "Remainder: " & seventy mod six
Yep, our fourth grade math skills are still evident. And although interesting, I still can’t think of a real world application for this.
So, let’s take a moment and explore AppleScript’s rounding rules. They are usually only in effect when coercing decimal data to an integer, which natively rounds to the nearest whole integer. The rules are very similar to what we learned in grade school. You can see that with this little repeat loop:
set x to 1
repeat 10 times
display dialog (x & " rounds to " & (x as integer)) as string
set x to x + 0.1
end repeat
You are not limited to only using built-in rounding, however. Look in the dictionary for the StandardAdditions under the Miscellaneous Commands section, and you see the command round. The definition briefly describes your various uptions, and the technical information provided by Apple is found here. You can use this command to round your non-whole numbers any way you want. For example, if you wanted every thing rounded up, you would do this:
set x to 1
repeat 10 times
display dialog (x & " rounds up to " & (round x rounding up)) as string
set x to x + 0.1
end repeat
As you can see in the dictionary and technical notes, there are some other ways to round your data as well, so play around with it.
It is also useful to note the order in which AppleScript performs mathematical tasks. Table A-8 (Operator precedence) of the AppleScript Language Guide (Click on Operators in The Language at a Glance and scroll to the bottom of the page) lists the order of 12 groups of operators. The first is the parentheses, second is number signs, then raising to a power, and fourth is multiplication, division, and div or mod. Here is an example of what this means:
2 ^ 2 * 2 --> Results in 8
2 ^ (2 * 2) --> Results in 16
Since the raising to a power is higher in precedence, the 2 ^ 2 will be performed first in the first example, and that result will then be mulitiplied by 2, which returns a product of 8. If, however, we enclose the 2 * 2 inside parentheses (as in the second example), it is then performed first, and that product (4) is used as the exponent for the first operation, which then returns 16.
Okay, I know that this is all terribly exciting, but even if you believe that you will never need to know this stuff, I promise you that you are wrong, and someday it will come in handy, if for no other reason than to make you stop and think (then refer to the reference for guidance). As you read through other people’s scripts, you will find some very clever uses of mathematical functions that help to either speed up a task, or simplify a method of analysis. Study these things, and see for yourself if these basic commands don’t help you as well.