Thursday, March 17, 2011

Apple Script: Hello World

The first script a person writes in a language tends to be a "Hello World" tutorial.
To write Hello World in Apple Script open the Script editor and type in:
display dialog "Hello World" and click the green arrow with run underneath it.

Script


The result of this will display this window:


Anything written within the double quotations " " will be thought of as absolute text meaning that the text is read as it appears, here is another example using a variable. A variable is something that stands something else. In math class when x=43, x was a variable.

Lets write another Hello World script but this time lets use a variable. Variables can be written as anything you want them to be but usually it is good to put them in the format of sometext_sometext. That is because we don't want to confuse them with any internal commands apple script has. Type the following in the editor:

set first_variable to "Hello World"
display dialog first_variable

Script

Run this script and once again this will come up with a box saying Hello World. We didn't use double quotations around first_variable, this is because we want what the variable is set to and not the variable name. Check out below what would happen if we wrote it with double quotations.

Script

We would get this result:

This is because display dialog used the text "first_variable" instead of the variable first_variable.

Now that you know how to display text and how to use variables we can move on to our next part.

2 comments: