Here is a quick overview of how to write and save AppleScripts with Script Editor. I've geared it towards working with iTunes. For the examples, I've used Script Editor v2.2 in Leopard; differences in older Script Editor versions are mentioned where necessary.
For more details, I recommend going through Apple's Introduction to AppleScript Language Guide.
(Portions of this article have been re-printed in the O'Reilly book iPod & iTunes HACKS.)
Shake Hands with Script Editor
Script Editor is an application included with AppleScript that lets you write, test, and save AppleScripts. In OS X it is in Applications > AppleScript.
When you run Script Editor and create a new Script Editor document (File > New) notice how similar it looks to a text editor like TextEdit:
The main window is where you will type in command statements. In the above picture you can see the cursor telling you where text will appear.
The main window of every Script Editor document contains four default toolbar buttons that affect that particular window. From left to right they are:
Stop - if the script is running it will attempt to stop it. Control-period also aborts the running of a script.
Run - activates the current script.
Compile - ("Check Syntax" in older versions) Script Editor will check the script for correct syntax before running it; this does not check if the script actually works, just whether it's constructed properly.
Bundle Contents - if your script is saved as a Script Bundle or Application Bundle (more on this later) this button opens a sidebar displaying the contents of the script's Resources folder.
Script Editor 2's toolbar is customizable with more commands, but these are the defaults.
Along the bottom of Script Editor are three tabs which determine what is displayed in the lower half of the window. "Description" contains a text description from the author of what the script does (I also use it for temporarily parking snippets of code or copy-and-pasted text). "Result" displays the last result computed by a script. "Event Log" is a running display showing the results of a running script and "logged" results; very handy for debugging.
For the purposes of this article we're going to create a simple script or two to use with iTunes. Then we'll go over how to save it.
Type this in your bare Script Editor window (use the "return" key to start a new line):
(I have adjusted my Script Editor's text formatting, so your text may not appear exactly like the above.) When you first enter text into a Script Editor window it's only text. Now click the Compile button in the toolbar and Script Editor will make sure the script's syntax is written correctly. If it is, Script Editor will compile it and format the text like so:
Okay! We have a script which we can Run and Save. Let's discuss how to save it and what sort of script to save it as on the next page...