AppleScript sales-tax calculator: Variables, operations, and comments

Post to Twitter

Welcome back to AppleScripting! So far, we’ve learned about what AppleScript is for, and said hello. Today we’ll really be diving into the language. We will talk about variables, datatypes, operations, comments, and some basic debugging. First I’ll introduce you to the concepts, and then we’ll look at a small example application: a sales-tax calculator. Let’s begin.

To start off, let’s talk about variables. Variables are basically a way to strore information within your script. You can, for example, do a calculation and save the result in a variable. This is helpful because it allows you to access that information later without doing the calculation again. If you want to learn more about variables, the Wikipedia article is a good source. Here is how you save, or define a variable in AppleScript:

set myVariable to 5
set is the keyword that you use to define a variable. The syntax goes like this: set variableName to data

So, what kinds of things can you store in variables? The most common ones in AppleScript are numbers, text, lists, and aliases. You know what numbers and text are, but what is a list? A list is like a combo-variable: it lets you save multiple pieces of data inside one variable. An alias in AppleScript is like an alias in Finder: a pointer to a file or folder.

The first type of variable, or datatype, we will talk about is the number. You can do various things to numbers, like add and subtract them. These things are called operations. In the code below, we define two variables and do some math with them.

set numberA to 5
set numberB to 9
set numbersAdded to numberA + numberB
log numbersAdded
Here we have defined three variables, numberA, numberB, and numbersAdded. We have also done an operation: we added numberA and numberB. We saved the result from that into our third variable, numbersAdded. Then, there is one more line that we have not seen yet: log numbersAdded. logging something saves it in AppleScript Editor’s log so that you can see what it was. To access the log, click the Events button near the bottom of the AppleScript Editor window. It should say (14). This kind of logging is a basic debugging method: it allows you to see what your script is doing.

Let us move on to some other operations you can do on numbers. Add this code to what you have already got from the previous example:

log numberA - numberB --subtraction
log numberA * numberB --multiplication
log numberB / numberA --division
Run this code and check the log. You should see the result of the math you have done. Note that numbers can be negative (the log entry for when we subtracted 9 from 5), and that they do not have to be whole numbers (like when we divided 9 by 5). There is one other thing to notice about this code: the gray text that comes at the end of each line. This text is a comment. That means that it is ignored by the script and only there for humans to read. I have used comments to annotate this code and remind us what each operation does.

Now let’s talk about text, the second major datatype in AppleScript. Text variables store, well, text. You can save text, use it later, combine it with other text, and more. You define a text variable the same way you define a number variable, except that the text you want to save needs to have double quotes around it. Replace your existing code with this:

set textA to "Hello"
You can use a text variable anywhere you would use text in quotes. Add this line to your existing code to try it out:
display dialog textA
The process of combining two pieces of text is called a concatenation operation. In AppleScript, you do this with the ampersand symbol. Remove that last display dialog line and add this:
set textB to "World"
log textA & textB
Check the log and you will see that our two text variables have been combined into HelloWorld.

In AppleScript, variables automatically switch from numbers to text as needed. You can display dialog a number variable and it will automatically be converted to text and displayed. Some other languages make you do this manually, but you do not have to worry about it with AppleScript.

Let us briefly talk about lists. Replace your existing code with this:

set fruits to {"apples", "bananas", "oranges"}
log fruits
log item 2 of fruits
set fruits to fruits & "pears"
log fruits
As you can see, the syntax for defining a list variable goes like this: {data,data}. Text has quotes around it as usual. You can retrieve a particular list item by using the item number of listVariable syntax. The first item of the list is item 1. You can add things to the end of a list by using the concatenation operation (with the &). Note that any of these things can be variables: You can add variables to a list or use a number variable as the item to retrieve.

Let’s briefly talk about the last major datatype, the alias. The easiest way to define an alias is to select the file or folder yourself. Replace your existing code with this to try it out:

set someFile to choose file
log someFile
tell application "Finder" to make new Finder window to container of someFile
So, the first line is fairly simple. The choose file command opens a standard OS X “pick a file” dialog and saves an alias. We are saving that alias into the variable someFile. The last line is the most complex. You saw the tell application code in the Hello World blog post, but this one is a bit different. For one thing, it is all on one line. You can do that if you only have one line to “tell” the application. The trick is to add the word to after the name of the application. Now let’s talk about what we are actually telling Finder to do: the make new Finder window code is, you guessed it, telling Finder to make a new window. The to and what comes after it tells Finder what part of the filesystem to open the window to. Here, we are telling it to open the container (enclosing folder) of the file you selected.

To wrap things up today, let’s write a little app that actually does something. This app calculates sales tax for an item. Replace your existing code with this:

set price to 12 --price of item, in dollars
set taxRate to 8 --tax rate, in percents

set taxRate to taxRate / 100 --turn percent into decimal set tax to price * taxRate set total to price + tax

set prices to {price, tax, total} --put price and tax into our list log prices display dialog "Your $" & item 1 of prices & " item plus $" & item 2 of prices & " tax cost $" & item 3 of prices & "."

Ta-da! Our script calculates the tax, adds it to the price of the item, and tells us the total.

You have now learned a lot more about AppleScript. You can store things in variables, know the different datatypes, and can do some operations. In the next post, we will talk about loops and program flow.

If you have any questions, feel free to comment on this post and I will respond. If you have any technical questions about AppleScript or AppleScript Editor, feel free to post on Ask Different!

Filed under AppleScript

One Comment

Subscribe to comments with RSS.

  • daviesgeek says:

    Cool little script Nathan! Thanks for sharing it!

  • Comments have been closed for this post