|
![]() |
Applescripting Plain and Not Very FancyBy Francine Schwieder |
Welcome to AppleScript, Apple's own scripting language, which lies somewhere between programming and macros in terms of what one can do with it. This was the topic of the last general meeting, and those of you who were there know that our presentation was even more of a disaster than usual. I will attempt to remedy at least some of the defects in our presentation with this month's column.
First off if you did a standard install of your system you got the major pieces of AppleScript installed automatically. But you will want more to get the most out of Applescript. So pop your system CD in its player and open it. If you have a recent release of the Mac OS you'll find a folder called "CD Extras" containing a folder of AppleScript Extras with the OSA Menu installer, the AppleScript Guidebook modules (which only work with OS 8.6 and greater), and more sample scripts to go along with the Automated Tasks and More Automated Tasks that are part of the standard OS installation. And in case you are wondering where that stuff is, you'll find it in the "Apple Extras" folder that is created during the default installation.
After you run the OSA Menu installer and restart your computer you will find a new item on your menu bar: a little "S" icon located between the clock and the application menu on the right side of your screen. If you click on this you will see that it is divided into sections, and includes an entry to launch the ScriptEditor, and a way to open the Universal Scripts folder and the Finder Scripts folder. Both of these folders are located in the Scripts folder inside of your System Folder, and you can also add folders for scripts for scriptable applications. The OSA menu is context sensitive, thus you get one menu when you are in the Finder, another when you are in a program. The one constant is the content of the Universal Scripts folder, where you store scripts you want to be available at all times. Each of these sections will display the scripts you make and save to the appropriate folder, so you can launch your scripts whenever you want without trying to remember where they are.
The easiest way to begin AppleScripting is to launch the Script Editor from the OSA menu and record a series of actions. When the Script Editor window opens you will see an area at the top where you can enter a brief description of your script, then a tool bar with four items: Record, Stop, Run and Check Syntax. So let's say we would like to have our computer automatically check our mail whenever we turn it on. Click "Record" and navigate to your email application and launch it, then tell it to get your mail. After that is done click the "Stop" button and examine the results. You'll see that the launching part has been recorded there just fine, but the checking the mail part didn't turn up. You will have to write this part yourself.
Looking at the section automatically generated you will see that it begins with 'tell application "Finder" ' followed on the next line by 'activate.' So we start a new section with 'tell application "Eudora" ' and a new line for 'activate.' Now what? In order to find out what commands an application understands we need to open its dictionary. So go to the File menu and activate the Open Dictionary command. You'll get a dialog box, and you can navigate to the application you want and select it. A new window opens with a list in the left pane of the commands that application understands, and by selecting an item you get an explanation of the command. The command 'connect' sounds promising, selecting it gives us this--
connect: Connect to the mail server and transfer mail
connect
[sending boolean] -- true to make eudora send queued messages
[checking boolean] -- true to make eudora check for mail
[waiting boolean] -- true to make eudora wait for idle time before checking
So let us type 'connect' as our next line. Then we actually have to know something to continue. The cryptic "boolean" means that word can have either of two values: true or false. We also need to know one AppleScript command, namely "set." So our next line will read 'set checking to true.' Finally we conclude with 'end tell.' Now click the "Check Syntax" button and Script Editor will check and format your work. Click the run button and your script will do its thing.

Now it is time to save our script. Go back to the Script Editor, go the File menu and select save. In the resulting dialog box you will want to name your script, save it as a classic applet, and navigate to the Finder scripts folder to save it there so it will be available as an OSA menu selection. If you really want it to run automatically make sure that the "Never Show Startup Screen" box is checked and store the script in your Startup Items folder in your System Folder. Everytime you start your computer it will automatically get your mail. If you want a choice uncheck the "Never Show Startup Screen" box and when your computer starts it will present you with a dialog box with the choices of Run or Quit.
You will want to acquire additional commands, which will go in the Scripting Additions folder of the System Folder. I have Jon's Commands, which is very standard, and a bunch of other things, such as Standard Additions, AppleTalk OSAX, Remote Access Commands...23 items in all. To acquire more information and download these useful goodies, as well as collect scripts to use and adapt, you should visit some AppleScript web sites. One place to start is Apple's own Scripting web site, and another excellent site is the AppleScript Sourcebook, which will give you information and a list of other links. So give scripting a try, you might find it both fun and useful.