You'll get your Mac news here from now on...

Help TMO Grow

Subscriber Login

Advertising Info


The Mac Observer Express Daily Newsletter


More Info

Site Navigation

Home
News
Tips
Columns & Editorials
Reviews
Reports
Archives
Search
Forums
Links
Mac Links
Software
Reports
Contact



by Stephen Swift


Writing A Script
July 2nd, 2001

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.

Run
Syntax:
on run
 --Insert your script here.
end run
Usage: To specify what lines in the script will be carried out when you run the script.
Example:
on run
    beep (*When your computer sees this command, it will beep.*)
end run
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.

Tell
Syntax:
tell someapplication
 --Insert your script here.
end tell
Usage: someapplication is any scriptable application.  The tell handler is used to direct commands to certain applications.
Example:
tell application "Finder"
    activate (*Brings the Finder to the front.*)
end tell
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
Syntax:
if something is true then
 --do something
else
 --do something if it is false
end if
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.
Example:
if 3 + 4 is 5 then
    beep 2 (*If 3 + 4 = 5 the computer will beep twice.*)
else
    beep 1 (*If 3 + 4 = 5 is false, the computer will beep once.*)
end if
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.

The Dictionary

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.
  1. On startup, I want to log onto the Internet.
  2. 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
end tell


These Topics Were Covered This Time:

  1. Basic AppleScript syntax.
  2. How to use an AppleScript dictionary.
  3. How to hand code a simple script.

New Topics To Be Covered Next Time:

  1. What AppleScript formatting can tell you.
  2. Dialog boxes and If handlers.


Comments or Questions? Is this column going to slow or fast for you? Do you want to script something, but don't know how? Do you need something explained or have a question about a script?  My E-mail address, stephen@macobserver.com, is open 24 hours a day, 7 days a week.


Most Recent AppleScript Columns

Break Your Script Into Pieces with Subroutines
June 26th

Repeat Yourself With AppleScript
June 12th

AppleScript Lists
June 5th

AppleScript Archives

Back to The Mac Observer For More Mac News!



Today's Mac Headlines

[Podcast]Podcast - Apple Weekly Report #135: Apple Lawsuits, Banned iPhone Ad, Green MacBook Ad

We also offer Today's News On One Page!

Yesterday's News

 

[Podcast]Podcast - Mac Geek Gab #178: Batch Permission Changes, Encrypting Follow-up, Re-Enabling AirPort, and GigE speeds

We also offer Yesterday's News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Software
Browse the software section for over 17,000 Macintosh applications and software titles.

Hardware
Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .