AppleScript for Beginners I - Getting Started & Script Editor

I - Getting Started & Script Editor

Congratulations on your decision to finally learn AppleScript!  I know how you feel; you have been enjoying your Mac for some time now, and have heard the wonderful tales of adventure associated with developing scripts that will finally make your system your servant.  I am happy to report that the stories you have heard are true.  Although AppleScript is easy to learn, and considered a high level language, it is remarkably versatile at reducing or eliminating repetitive tasks, organizing files and projects the way YOU want, or moving data automatically from one application to another.  Plus, it is fun!  So welcome aboard and prepare to enjoy yourself as you learn.

Of course, for you to succeed, you need a comfortable working environment, besides your soft chair and wrist pad.  Before you dive into full time scripting, let's take a few moments to set your machine up for optimal usage, along your own personal guidelines.

The first step is to open a Finder window and highlight ‘Applications.’ If your Finder is set up for column or list view, you will see a folder entitled ‘AppleScript’ near the top of the Application folder listing. Highlighting the AppleScript folder will display a few items, only two of which we are concerned with right now. First is the ‘AppleScript Utility,’ which is an application that you want to activate immediately, if you have not already done so. This program essentially sets up your user for scripting. When you double click the utility, you will be presented with a dialog box containing these items:

Default Script Editor: (Leave it as the default, Script Editor, for now.)

GUI Scripting: Click the little box next to ‘Enable GUI Scripting’ so that a check mark appears in the box.

Folder Actions: (Ignore this one today; that will come later.)

Show Script Menu in menu bar: Check this one as well. Right away, you will see a little black scroll icon appear in the menu bar across the top of your screen. If you don’t know which icon it is, just keep clicking the box off and on until you notice it. This is a very handy little menu item that will give you access to all your scripts at any time, in any application. You will love it.

Show Library scripts: For now, please check this one also. It is a matter of personal preference, but until you use it for a time, you won’t know if you prefer it or not.

Show application scripts at: You can choose to have your scripts that are specifically written for certain applications either at the top or the bottom of your Script Menu. Again, it is personal preference, so if you don’t know, flip a coin, and pick one.
Well done; your first task is now complete. Click the little red dot in the upper left corner of the window to quit the utility. You can run this setup at any time to alter your settings, so feel free to come back later to make changes.

The second task is even simpler. You should now have the list of contents of your AppleScript folder before you again. You will see the ‘Script Editor’ application at or near the bottom of your list. For future usage, drag that icon right now down to your dock, so you can use it without needing to go into your AppleScript folder all the time. You may now close the Finder window.

Perfect.  We are moving along nicely, and almost finished.  Go on down to your dock, and click on your Script Editor icon to activate it.  You now have a nifty blank slate to write your scripts, compile them, and run them.  You will see that the Editor window is divided into an upper and lower section.  The upper section is where you will write the script, and the lower section is where you will see different things, depending on which of the three lower blocks is highlighted.  For now, we will only mention the two on the right side: [b]'Result'[/b] and [b]'Event Log.'[/b]  Result is the default selection, and will simply display the result in that lower window of any script that is executed.  If you select Event Log, you will see the various events happening (in real time) while your script is running, which can be useful when trying to find problems.

All right, back to the upper window.  Along the top you see 5 icons; 3 on the left of a vertical dotted line, and 2 on the right.  These icons are like buttons; one click and it makes something happen.  If you click the [b]'Record'[/b] button, Script Editor will try to track your mouse and keyboard movements and write a script describing those actions.  Unfortunately, very, very few applications on the Modern Mac are recordable, so you won't be using this button much.  (If you want to have some fun, however, click 'Record' and switch to the Finder and start doing things.  When you have had enough, click the 'Stop' button in Script Editor.  You now have a very weird script.  OK  Back to the buttons.)  The [b]'Stop'[/b] button simply stops the script, regardless of whether it is finished executing or not.  The[b] 'Run' [/b]button runs the script.  (If you did create a script with the Record button, and it is still there, click Run now and see what happens.)

Moving to the right side of the dotted line, you see the [b]'Compile'[/b] button, represented by a hammer.  (I have often wondered about that hammer...)  It is always a good idea to compile a script before running it, just to see if it will actually compile.  (More on that momentarily.)  You should also know that you can compile a script by pressing your 'enter' key on your keyboard. Note that [u][i]THIS IS NOT YOUR RETURN KEY[/i][/u].  If you are working on a Mac laptop, the enter key is usually the little key to the right of the space bar and "Apple" key.  If you have an Apple keyboard, it is the bottom right key of the number pad group, over on the right.  You will find this shortcut extremely handy, and may never even use the Compile hammer button.  The furthest icon over to the right is ghosted out, and labelled [b]'Bundle Contents.' [/b] Ignore that one for now.

Now that you are familiar with the general appearance of the Script Editor palette, you are ready to make it work YOUR way.  With Script Editor activated, go up to the Script Editor menu, click on Script Editor, and then click [b]'Preferences.'[/b]  The only two preferences we will deal with today are [b]'Editing'[/b] and [b]'Formatting.'[/b]

When you choose the Editing button, you have a small number of options to consider.  I typically leave the Line Wrap and Tabs settings alone, but you can change them however you like.  These setting will become more obvious to you as you write longer scripts.  The automatic indenting is a very nice feature, and you can certainly increase or decrease the indent number as you desire.  The Tabs in Editor don't really do much of anything, and I have personally found the Script Assistant to be more annoying than useful.  Essentially, what the Script Assistant does is to try to anticipate the commands you are typing in the Editor window.  That's fine, but the choosing of the prompted commands is time consuming, in my opinion.  You have to press the F5 key, scroll to the desired command, and then press 'return.'  I prefer just typing along.  Try it out for  yourself and see.  I would actually love to hear your input on this feature.

The Formatting section is where the real customization action exists.  Here is where you can set up the fonts used in Script Editor completely in accordance with your own personality.  (However, one cannot alter the background to anything but white.  Rats.)  The leftmost column lists the different categories of AppleScript text that the Editor does a GREAT job of keeping separated for you.  The three right columns allow you to choose whatever font, font size, and color you wish for each category.  Believe me, this is real power!!  Remember about compiling a script before running it  Look at the top line; it says [b]'New text (uncompiled)'[/b]  Whenever you type new text of any kind into Script Editor (even if you are working on a previously compiled script) the new text is displayed according to the font parameters shown on this line.  Once you compile the script, Script Editor can recognize whether your new text is a command, a keyword, a comment, a value, a variable, or whatever.  When it has resolved everything, it will change all the text to display different script terms according to these settings.  [i]THIS IS NOT THE SAME AS DE-BUGGING[/i], but it is the first step.  Very many scripts will compile just fine, but will not run.  Still, a successful compilation will help you to see visually if everything is how you had planned, even if you are color blind.  You can choose any font that is installed on your machine, and set the size of that font to anything you like.  Let yourself go.

I think we have spent enough time on Script Editor.  There are still a few interesting features to explore, but we can do that later.  Let's get started with some actual scripting!  Go to top window of Script Editor and type the word 'beep.'  (Ignore the quotes and dot. - You can also just click the [b]'Open this Scriplet in your Editor'[/b] line in the box below.)
Now compile it, watch the text change, and run it.

You should have heard a single audible of whatever your default system alert sound is.  If you did not, make sure your sound volume is turned up high enough to hear, and do it again.  Then, add the numeral 3 directly after the word 'beep' (no spaces) and compile again.
Did you see how the uncompiled numeral 3 was a different font from the compiled 'beep' and note the new color change on compilation  Try running this script.  You get the error message that the variable beep3 is not defined.  Now, try inserting a space between beep and 3, like this:
beep 3
The font colors change again, and when you run it, you hear your system sound off 3 times.  Now, add this line under the beep line:

display dialog “Hello World! How about some more beeps”

Then, select your beep line, copy it, and paste it (just like you would in any other text editing application) below the display dialog line:
beep 3
display dialog "Hello World!  How about some more beeps"
beep 3
Do you see how the text colors only change to the correct colors after you compile  If you are confident of your syntax, you can just press Run; the script will compile first, then run, so try that out as well.  Run this script a couple of times, first click OK, then click Cancel and note the different behaviors.  Now, go down to your lower window in Script Editor, and select [b]'Event Log,'[/b] and run it again, both ways, to see what the log shows.  Sometimes, as you will learn along the way, using the Event Log to 'watch' a script performing will assist you in finding those buggy areas that are not working the way you want.  Remember, however, that observing the Event Log will slow down the execution of a script, especially when it involves processing lots of data or reading files.

Once you are satisfied with your work, or you have made some progress writing or de-bugging, you need to save the script.  Saving a script is just like saving in other Aqua applications; click on either [b]'Save'[/b] or [b]'Save As'[/b] under the File menu (or their equivalent keyboard shortcuts), and you are presented with a Finder-like window.  Of course, you can save your scripts where ever you like, but remember that little black scroll near the top of your monitor  If you want your script to be available there, you need to select your user name in the far left column, then your Library folder, then the Scripts folder.  Any scripts saved in that folder will be accessible by clicking that little black scroll icon.

Now you have some idea about how the Script Editor can help you to recognize proper term usage, and to follow the execution of a script as it moves along.  If you are interested in trying to learn more about the terms that AppleScript uses, you are welcome to download the [url="]AppleScript Language Guide[/url] directly from Apple's website.  It is not current, but it is still useful, albeit rather large and intimidating.

Next time, we will explore a little more with Script Editor, AppleScript dictionaries, and learn how to write a script for an application, using iTunes as the sample target: [url=]See: AppleScript Tutorial for Beginners II - Variables and Dictionaries[/url]