AppStorm
3/16/12 5:18 PM
3/16/12 5:18 PM
pleScript folder within the Applications folder. You should see a simple window containing a large text field with a strip of buttons along the top. Inside the text field type the following code:
tell application "Finder" 1 2 display dialog "Hello 3 World" 4 5 end tell
AppleScript attempts to use plain English wherever possible to make coding extremely simple. Most commands in AppleScript are located inside a tell block. Its called a tell block because you are telling a given application what you want it to do. For instance, the code above is telling the Finder to display a dialog window containing the words Hello World. After you are finished with a command or string of commands for a given application, you end the block with end tell. Always remember to end your tell blocks correctly or the code will not compile! After you are done entering the code above, click on the Compile hammer icon. If your syntax is correct, your code will automatically format and colorize. If you have made an error, Script Editor will highlight the problematic area and give you a message about what it thinks might have gone wrong. Here is a quick reference to the various colors youll see in your compiled code (found in Script Editor>Preferences).
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-applescript/
Page 2 of 12
3/16/12 5:18 PM
Color Guide After your code has compiled, click on the Run button. You should see the following dialog:
Hello World Now click the OK button and look at the bottom of your Script Editor window. When you run a script, Script Editor tells you what the result was, or what was returned. In this case its telling you that the OK button was clicked.
The OK Return
Declaring Variables
Variables are essentially the same in every programming language. They provide an easy way to access and manipulate lots of information in a compact snippet of code. Creating or declaring variables is different for every language. In AppleScript you declare variables as follows:
set theString to "Hello World" 1 2 tell application "Finder" 3 4 display dialog 5 theString 6 7 end tell
There are several things to note about the previous example. First, notice that variables are declared using the set and to commands. By doing this you are setting
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-applescript/ Page 3 of 12
3/16/12 5:18 PM
your variable name, in this case theString, to equal something, in this case the text Hello World. Many programming languages require that you state the type of variable you want in the declaration (integer, floating point, text, etc.). AppleScript however, is intelligent enough to work with your variables without any instruction about the format. Also notice how I typed my variable name. You cant have spaces in a variable name so its good practice to use camel case (theString) or the underscore method (the_string). It doesnt really matter which method you choose, just make sure youre consistent throughout your code. Its also a good idea to give all your variables meaningful names. When you are looking another programmers code, it can be annoying to see variable names like myVariable that dont give any indication as to what they are or what they will be used for. Finally, notice that now that Ive placed the text Hello World inside a variable, I can call that variable again and again throughout my code. Then if I later decide to change the text Hello World to Good Morning Dave, I only have to change the text on the line where I declared the variable.
3/16/12 5:18 PM
You can compile your code quickly by pressing the enter key (not the return key). This is located on the ten key number pad on desktop computers and next to the Command key to the right of the space bar on laptops. As your script becomes more complex, a bit of organization is in order. By typing two dashes before a line of text, you can insert a comment. Use comments to separate and describe your sections of code for easy navigation. In this example Ive created a string variable (text only) and a few integer variables. Notice that you can perform mathematical operations on variables. Here Ive set theFirstNumber to equal three and theSecondNumber to equal two and then added them together in theAnswer. Also notice that you can change a variable after it is declared. Immediately after setting theAnswer to the sum of theFirstNumber and theSecondNumber (resulting in 5), I changed the value of theAnswer by adding one to it (resulting in 6). If you run this script you should see the following result:
Some Basic Math Again, this only scratches the surface of the kinds of operations you can perform on variables. For now you should just understand that a variable isnt static. Much of the power of behind any programming language is the ability to manipulate variables to perform a wide variety of tasks.
3/16/12 5:18 PM
The Mail Dictionary The column on the left contains the available Suites of commands and items. When you click on a suite, youll see everything contained in the suite displayed below. You can narrow this preview by clicking in the second column, then again in the third. Suites contain commands (C with a circle) and classes (C with a square), classes contain properties (P) and elements (E). To understand how all this works, lets use this dictionary to create a script.
3/16/12 5:18 PM
as who the message is sent to. As we write our algorithm, we need to keep in mind the way AppleScript works. Here are the steps I came up with: 1. Create variables for the recipient, the recipients email address, the subject of the email, and the text for the body of the email. 2. Create a variable that holds our new message along with its various properties 3. Create the new message 4. Send the new message
As you can see, weve just put placeholder text into the variables for the name and email address of the recipient as well as the subject and content of our message. You can change these to anything youd like. Be sure to put your own email address in the recipientAddress variable so you can ensure that the the script is working properly when you receive the email.
3/16/12 5:18 PM
want! Now click on the outgoing message class and look at the available properties down below. We need to plug in our variables for the recipients name, the recipients email address, the subject, and the contents of the message. In the list of properties there isnt anything about the recipient but there are properties for subject and content. We now know the proper syntax to refer to these properties. Notice that the dictionary gives you the format to define the properties. For instance for the subject, well type the word subject followed by a colon followed by the text of the subject.
Subject Content Also in this suite youll find a send command, which we can use to send the message by simply typing send. We still need to know the proper syntax for the recipients name and email address. Since its not in this suite, click on the Message suite.
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-applescript/ Page 8 of 12
3/16/12 5:18 PM
About halfway down the list of classes we find recipient. Click on the recipient class and we see that once again, we can use plain English to refer to the properties of the recipient. Well simply type name and address. You can use the search feature to hunt down properties, classes, elements and commands quickly. Now we are ready to create our message variable using the syntax weve just learned. Heres what the code looks like:
--Variables 1 set recipientName to "John Doe" 2 set recipientAddress to "nobody@nowhere.com" 3 set theSubject to "AppleScript Automated Email" 4 set theContent to "This email was created and sent using 5 AppleScript!" 6 7 --Mail Tell Block 8 tell application "Mail" 9 10 --Create the message 11 set theMessage to make new outgoing message with properties 12 {subject:theSubject, content:theContent, visible:true} 13 end tell
Notice Ive created a tell block to enclose all the commands to the Mail application. Then I set a variable (theMessage) to make a new outgoing message with the properties discussed above. Also notice that sets of properties are always contained in brackets { }.
The Final Step: Setting the Recipient and Sending the Message
Now that weve created our message variable, we need to call that variable and create a new message with the properties of theMessage. We also need to set the recipients and send the message. To do this, well use a tell block on our variable. Heres our finished script.
--Variables set recipientName to "John Doe" set recipientAddress to "nobody@nowhere.com" set theSubject to "AppleScript Automated Email" set theContent to "This email was created and sent using AppleScript!"
Page 9 of 12
1 2 3 4 5
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-applescript/
3/16/12 5:18 PM
5 AppleScript!" 6 7 --Mail Tell Block 8 tell application "Mail" 9 10 --Create the message 11 set theMessage to make new outgoing message with properties 12 {subject:theSubject, content:theContent, visible:true} 13 14 --Set a recipient 15 tell theMessage 16 make new to recipient with properties 17 {name:recipientName, address:recipientAddress} 18 19 --Send the Message 20 send 21 end tell end tell
First, we created a new copy of theMessage (which inherits all the properties weve put into it) and set it to recipient with properties. This tells Mail that we want to add a recipient with the following properties. Here we just used the syntax we learned before and the variables for the name and address of the recipient. Finally, we invoked the send command to send our message. Notice that we have two tell blocks to close this time. Once youve compiled your code and fixed any errors hit the Run. Mail should automatically create and send the message. Tadaah! Check your sent folder to make sure everything worked.
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-applescript/
Page 10 of 12
3/16/12 5:18 PM
Mail Message Congratulations, youve created your first AppleScript! You can save it as a simple script that you can come back and edit or as an application that runs automatically when you open it.
3/16/12 5:18 PM
free). Please feel welcome to leave a comment and let us know if you found this tutorial helpful! What other AppleScript tips would you like to see covered in the future?
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-applescript/
Page 12 of 12