Cole McDonald's
AppleScript Studio Tutorial

AS Lessons | AS Studio Lessons | Element Short List | Element Full List | Container Views | Data Sources | Menus


Back To Table Of contents


My Cup Runneth Over With Information
-Variables-


This will be the first, but not the last, technical part you will need to not only learn, but master. Now don't get scared by this, I'm going to take it one step at a time.

Variables are places to store information (data). I like to use the example of a cup. If you can imagine an empty coffee cup, you can learn how to use variables. If you could get:

  • a cup of some sort
  • a pencil or pen
  • a piece of paper

    I'd like to do this hands on. I'll wait...

    Okay, now that we've got our materials, I'd like you to write your first name on the pice of paper with the pen or pencil (or crayon-yeah, I was watching). This piece of paper with your name on it is our datum (that's one piece of data. Data is a plural). Now, using the cup, figure out a way to move the datum from one side of your desk to the other without touching the paper when you move it.

    Now, the easiest way to do this was to put the paper in the cup and move the cup to the other side of the desk. Yes, I know this is silly, but it makes a point. Programming is all about moving data from one place to another. the way we do this is by using coffee cups. Well, not really coffe cups, but something like them. You can't touch them or see them moving stuff around like you can with the cup. We call these cups VARIABLES. We will use variables in everything we do as programmers.

    In fact, we've already seen them. We used one in the last example about getting information from the user. I'm sorry, but I snuck that in with out telling you. I'm naughty. If you recall, we got the user's first name and then displayed it to them again using a display dialog command. We used the variable firstName to move the information from the first dialog to the second. It didn't actually have to be called firstName to be able to hold a firstName, I just used that to make it easier to read.

    Historically, back when I was learning to program in BASIC (Beginners All-purpose Symbolic Instruction Code), we used to use variables named a, b, c, d,...z. Now, these were just for numbers, for letters and words (strings), we had to use a$, b$, c$,...z$. If you filled these up with data, you very quickly forgot what was in which variable. I used to keep a list of variables and what they contained in a notebook that I carried with me all the time. That made it easier to look up the purpose of a variable once I forgot what it was for. Especially if b$ was the first name of the user in one program, but their pet's species in another. That would be embarrassing to get those mixed up. "Excuse me, Mr. Shi Tzu Johnson?"

    Now when we name variables, we can give them names that make sense. In our example, we used firstName. This gives a pretty good idea of what kind of datum is stored in the variable. You could choose to call it fred if you wanted, but that doesn't necessarily make as much sense. When you get to making larger programs, you start to deal with dozens of variables that you need to keep track of. It makes sense to name them appropriately.

    Now data is a funny thing. It comes in may shapes and sizes. Sometimes you will want to do math with it (Ooh, he said the M word). Many times, you will have more than one piece of data to store (remember what one piece of data is called? - Datum). Sometimes you will use them to build sentences (again, more school stuff). These are all stored differently. Lets start with the words and sentences since we already have done some of that with the firstName variable from the last section.

    Words and sentences (text) are stored in a special kind of variable called a string. Deep inside, the computer sets aside a block of memory as long as the word or sentence you are storing in the variable. The memory works this way - Inside your computer, there are millions of little light switches. Eight of these switches grouped together is called a byte. One byte of memory can store one character. A character is considered to be a letter, a number, or a white character (space, tab, return, etc), and punctuation. Capital letters are considered to be different letters than lower case letters. These are all arranged in a character set called ASCII. This lets all computers talk a common language when it comes to text.

    Text is stored in a variable type called a string. You can turn a variable into a string by putting (assigning) data into it like this:


    set thisVariable to "This is a string."

    Numbers that you will be doing math with will be stored in a couple of types of variables. These are:

    You can set these using the following methods:


    --number
    set thisNumber to 5.7 as number
    --integer
    set thisInteger to 5 as integer

    You will notice that I specifically told the script that the variable was a specific type using as number or as integer. You can do the same thing with any of these data types:


    set definatelyAString to "This is most certainly a string!" as string

    Now it would be convenient to be able to store a bunch of information in one place to call up. Many of these programming tutorials will use an address book program example or a CD/DVD collection program to show how to do this, but I'm going to stay with the example we have already started. If we wanted to get the user's first name, last name and year they were born, we would probably want to store these all in one place. If you were to get a piece of paper from everyone in your school with their first and last name and their year of birth to store in alot of cups, you would certainly want to put each person's information in a cup rather than scattering it all over the floor. it would keep it much easier to get to later.

    This variable type is called a list. It works this way:


    set firstName to "Fred" as string
    set lastName to "Jones" as string
    set birthYear to 1990 as integer
    set peopleList to {firstName, lastName, birthYear} as list

    we use a special set of characters to put our pieces together into the list. This is usually called a Curly Brace. They come in pairs...ALWAYS! If you don't use them in pairs, your script will break in really bad ways. Not like smoke pouring out of the computer bad, but spending an hour to find out you forgot to close your list can be upsetting.

    In order to get information out of a list, we have to treat it in a special way. The variable type is a list, but the variables inside it are called items. You would get to the information in the last example this way:


    set usersLastName to item 2 of peopleList

    Let's put the lessons together to get something useable. Our goal at this point is to ask the user for their first name, last name and date of birth.


    --Gets the user's first name.
    display dialog "Enter your first name:" default answer ""
    set firstName to text returned of the result

    --Gets the user's last name.
    display dialog "Enter your last name:" default answer ""
    set lastName to text returned of the result

    --Gets the user's year of birth.
    --Note the part in parantheses (yyyy),
    --this tells the user how we want them to enter the answer to the question.
    --This says we want a year that looks like - 1971 rather than 71
    --(yes, I was really born way back then).
    display dialog "Enter your year of birth (yyyy):" default answer ""
    set birthYear to text returned of the result

    --Grab the text returned from the result of the previous command
    set personList to {firstName, lastName, birthYear}



    Now that we know how to store our information, what do we do with it?

    Next Lesson - Too Much To Choose From, Which Candy Bar Should I Get? - Decisions