Lesson 1 - Comments and Variables - Applescript
First off, open up AppleScript Editor on your Mac. It is located in your "Applications" folder under "Utilities". Let's get familiar with AppleScript editor. Create a new document and you will notice many things.
On the top, there is "Record", "Stop", "Run", "Compile", and "Bundle Contents". The only ones we will be using throughout this tutorial are "Stop", "Run", and "Compile".
In the middle of the window, you will notice a large empty white space. This is where you will be writing all of your scripts. Above the white space is some drop down boxes that can tell certain programs things. This is also something that we will not use during this tutorial.
At the bottom, there is another white space. This area, if set to "Event Log", will tell you what is going on in the program while it is running. Play around with this as you wish. It can be useful in debugging, but is not needed as of now.
Now for the fun stuff! Today, we will create our first program. In order to do this, however, we must know how to use comments and variables.
The following is a comment:
On the top, there is "Record", "Stop", "Run", "Compile", and "Bundle Contents". The only ones we will be using throughout this tutorial are "Stop", "Run", and "Compile".
In the middle of the window, you will notice a large empty white space. This is where you will be writing all of your scripts. Above the white space is some drop down boxes that can tell certain programs things. This is also something that we will not use during this tutorial.
At the bottom, there is another white space. This area, if set to "Event Log", will tell you what is going on in the program while it is running. Play around with this as you wish. It can be useful in debugging, but is not needed as of now.
Now for the fun stuff! Today, we will create our first program. In order to do this, however, we must know how to use comments and variables.
The following is a comment:
#This is a single-line comment!
(*
This is a multi-line comment!
*)
Comments are not read by the program and are used only to make notes inside of your code. Sometimes while programming, you will forget what a certain section of code does. Although this seems unlikely, a 15 line program can easily become a 500 line program with a few small additions. Commenting will allow you remember what certain sections of code do and it helps others read your code as well.
Now we continue onto variables. Variables are used to assign values to words. There are a few rules to keep in mind while creating variables. For the most part, it is standard to create variables like "variableOne" or like "test3". These follow the convention of two-word variables that start with a lowercase word and have a capitalized second word or have a number. This prevents any chance at using a variable that is actually a command. Note how the two examples both start with letters. It is NEVER acceptable to start a variable with a number. AppleScript editor will give an error if you try to compile a variable like "1a".
Enter the following code into AppleScript Editor:
Now we continue onto variables. Variables are used to assign values to words. There are a few rules to keep in mind while creating variables. For the most part, it is standard to create variables like "variableOne" or like "test3". These follow the convention of two-word variables that start with a lowercase word and have a capitalized second word or have a number. This prevents any chance at using a variable that is actually a command. Note how the two examples both start with letters. It is NEVER acceptable to start a variable with a number. AppleScript editor will give an error if you try to compile a variable like "1a".
Enter the following code into AppleScript Editor:
set theVariable to "Hello, World!"
Go ahead and run this.
Congratulations! You just created you first AppleScript program!
You will notice that it seems to do nothing. However, in the debugging section, you will notice that the result is "Hello, World!" This means that AppleScript just associated the value of "Hello, World!" the word theVariable.
The next thing we will discuss is VERY important.
If you didn't already know, variables can be different types. For example, "1" and 1 are not the same. AppleScript recognizes "1" as a word, and 1 as an integer. When receiving user input (discussed in lesson 2), the returned is always a "string" (word). In order to change this, one would have to do something like this:
Congratulations! You just created you first AppleScript program!
You will notice that it seems to do nothing. However, in the debugging section, you will notice that the result is "Hello, World!" This means that AppleScript just associated the value of "Hello, World!" the word theVariable.
The next thing we will discuss is VERY important.
If you didn't already know, variables can be different types. For example, "1" and 1 are not the same. AppleScript recognizes "1" as a word, and 1 as an integer. When receiving user input (discussed in lesson 2), the returned is always a "string" (word). In order to change this, one would have to do something like this:
#Example 1
set a to "1"
set b to a as integer
#Example 2
set x to 1
set z to x as string
This is how you would change the variable type. Remember, "string" is words and "integer" is numbers. You can't compare words and numbers, because 1 ≠ "1".
In the next lesson, we will learn how to display words and variables in a GUI (Graphical User Interface).
In the next lesson, we will learn how to display words and variables in a GUI (Graphical User Interface).