Apple's first plan for AppleScript was a little different from what we know it as today. Plan A: To create an interface to control automated tasks through a series of instructions without requiring the user to know how to write these instructions. Plan A failed. Plan B: To create a programming language that would allow users to easily write these instructions using an English syntax style. This, in essence, is what AppleScript is. Parts of plan A, such as the record button, are still included in AppleScript. We used that feature in the previous article. Plan A failed because the lack of support for recordable applications. If an application is recordable, AppleScript will create a script by watching what you do. Few applications have this functionality. What you can accomplish by recording your actions is limited. You can't have the script make decisions. It can only repeat what you just did. You may end up with an incomplete script, because the commands it can record are limited. So, yes you do have to write code, but if you understand that nouns, verbs, and adjectives make up sentences, it will be easy. Hopefully, you will also find it fun, interesting, and satisfying.
The Core of a Script
Think of a script as an outline for a document. You have your main topics, secondary information, and some notes. In AppleScript, they are called handlers, commands, and comments. The three most commonly used handlers are run, tell, and if. The name handler comes from the idea that they control or handle all the commands in the section. There are many others, and you can make your own, but for now we'll stick with the basics.
--Insert your script here.
Usage: To specify what lines in the script will be carried out when you run the script.
beep (*When your computer sees this command, it will beep.*)
Noteworthy: AppleScript calls the run handler automatically when it runs a script. Most of the time, it isn't necessary to include it in your script.
--Insert your script here.
Usage: someapplication is any scriptable application. The tell handler is used to direct commands to certain applications.
tell application "Finder"
activate (*Brings the Finder to the front.*)
Noteworthy: The application must understand the commands directed towards it. The Finder may know what select startup disk means but Microsoft Word won't.
if something is true then
--do something if it is false
Usage: This is how AppleScript makes decisions. Something could be a mathematical equation, application, or about most anything else. You wouldn't use the word true in your if statement. You would put something there to compare it to the first item. When AppleScript runs the script, it will determine if the line is true or false. This is called a Boolean.
if 3 + 4 is 5 then
beep 2 (*If 3 + 4 = 5 the computer will beep twice.*)
beep 1 (*If 3 + 4 = 5 is false, the computer will beep once.*)
The Result: Because 3+4 = 7 not 5, AppleScript will return the statement as a false Boolean and beep once.
Noteworthy: The if statement is the core of most scripts. It is a very useful and complex handler. However, all if handlers are variations of this basic syntax.
A regular dictionary will tell you that the word egabando doesn't exist. An AppleScript dictionary will tell you what commands applications recognize. This is how you tell what commands you can put in your tell handlers. Drag an application onto Script Editor. If you dropped Quake III onto it, you probably would have gotten the message, "Could not read the dictionary of the application or extension because it is not scriptable." This means the application isn't scriptable, and it doesn't recognize any command. If you dropped the Finder onto it, you would have gotten a window with many commands. There are different parts in a dictionary. Right now, use the dictionary to determine if an application is scriptable, and to figure out what it understands.
A Useful Script
You write a script because you want something done. Everyday I open the same web pages. Wouldn't it be nice to have the pages load automatically? Before you dive into the code, think about what you want this script to do exactly.
- On startup, I want to log onto the Internet.
- Open macobserver.com, macaddict.com, and appleturns.com
Number one can be accomplished by placing the script in the startup items folder. I use Internet Explorer, but Netscape and iCab are both very scriptable. Drag your browser onto Script Editor. Look for the command OpenURL on the left side of the window. When you find it, click on it. At the top of the right panel should be a description about the command. "Retrieves URL off the Web" sounds like what we want. The dictionary also gives us some options. Where do you want this URL to go? We want a new window for each of the pages. We'll use the toWindow command. The toWindow command needs an integer (i.e. 1, 2, 3) to specify what window the page will load into. The dictionary tells us 0 means new window, so we'll use that. If we put the information given to us together, a standard OpenURL command would look like this: OpenURL "a URL" toWindow #. Copy that line. We now have everything we need to create our script.
Type in tell application "Internet Explorer"|"iCab"|"Netscape". I'm using the | to separate your options. If you use iCab, you would just type in "iCab." If you are using the formatting options I suggested in the previous article, the text should appear red to represent new text. Programmers like to say uncompiled, a fancy term for new text in a program. This means AppleScript hasn't checked your work yet. Paste the default OpenURL command into your script. Now change "a URL" to "http://www.macobserver.com" and change the # to 0, for a new window. Repeat these last steps, until all your URLs are set up. Then type end tell on a new line to end the handler and your script. Save your script as an application. Make sure Never Show Startup Screen is checked. Place your application in the Startup Items folder. My finished script looks like this:
tell application "Internet Explorer"
OpenURL "http://www.macobserver.com" toWindow 0
OpenURL"http://www.macaddict.com" toWindow 0
OpenURL "http://www.appleturns.com" toWindow 0
These Topics Were Covered This Time:
- Basic AppleScript syntax.
- How to use an AppleScript dictionary.
- How to hand code a simple script.
New Topics To Be Covered Next Time:
- What AppleScript formatting can tell you.
- Dialog boxes and If handlers.