The "NEW" Script Editor
Volume Number: 20 (2004)
Issue Number: 4
Column Tag: Programming
by Benjamin S. Waldie
The "NEW" Script Editor
A few months ago, Dave Mark wrote a couple of articles about AppleScript. These articles touched briefly on topics including setting up the script menu in Mac OS X Panther, performing some basic Finder scripting, and accessing application dictionaries. This aims to be the first in a series of articles geared toward providing a more in-depth look at AppleScript.
Figure 1. The Script Menu's About Screen
Primary interface changes
This month's article starts at the beginning, and focuses on the new features in the new version of the Script Editor application, available with Mac OS X Panther (10.3). Those familiar with the Script Editor know that it has had some limitations over the years. For example, the Script Editor never had a find and replace, it had a limit on the number of characters that could be entered into a script document, and it was, of all things, not AppleScriptable. All of this has changed with the release of Script Editor 2.0!
The Main Document Interface
The first thing that you will notice when you launch the new Script Editor, is that the interface looks different than that of previous versions. Since the entire application has been rebuilt from the ground up as a native Mac O S X application, it now sports this new look and feel.
The Script Editor is located in the /Applications/AppleScript/ folder on your hard drive. Several other items exist in this folder as well, including a folder filled with fully editable example scripts.
Figure 2. The Script Editor's Document Window
First, let's take a look at the document window. You will notice that the top of each document window contains a toolbar. By default, this toolbar contains the expected buttons, including "Record", "Stop", "Run", and "Compile" buttons. Like many other Mac OS X applications, users now have the ability to customize the toolbar and add some other handy buttons for quick access. This can be done by selecting Customize Toolbar from the View menu.
The next thing that you will notice immediately is that the Description, Result, and Event Log are now located at the bottom of the document window. By clicking a tab, or by typing command + 1, 2, or 3, you can quickly and easily cycle between these views. If you prefer to have more space for editing your script, simply double click the window divider, or drag it to the desired height, to reduce the size of the tabbed area.
As in previous versions of the Script Editor, new document windows open to a predefined size. The default size of a new document window is still configurable, although Apple has moved this configuration to a new location. To set the default size of a window, first scale a window to the desired size. Next, select Save as Default from the Window menu.
The Navigation Bar
Another new feature, which you may not notice right away, is a navigation bar, located directly above the code area in a script document window. If you don't see this navigation bar, select Show Navigation Bar from the View menu in order to make it visible.
Figure 3. The Navigation Bar
The navigation bar contains two popup menus. The first popup menu displays the current scripting language. The second popup displays a list of all of the elements in your script, including properties, globals, handlers, and script objects. By selecting an element in this popup list, the Script Editor will automatically navigate to that element and select it for you. In a script containing a large number of handlers, this can prove extremely useful. Unfortunately, there is not currently a way to sort this list of elements alphabetically. Rather, the elements are sorted in the order that they appear in the script.
A handler, also referred to as a subroutine, is a modular "chunk" of code that you write to perform one or more specific tasks. Once written, a handler may be called from multiple places in your script to perform the desired process. Handlers help to eliminate repetition in your code. By breaking your code up into smaller pieces in this manner, it also help to make your code more organized, easier to debug, and easier to reuse in future scripts. We will review handlers in more detail in a future article.
Result History and Event Log History Windows
The Event Log and Result tabs at the bottom of each document window display only the result or event log for the last time the current script was run. However, developers may need to retain this information for testing purposes.
In order to accommodate this need, the new Script Editor now contains an Event Log History window and a Result History window. These windows may be made visible by selecting them in the Windows menu. In order to function, they may also need to be enabled under History in the Script Editor's Preferences. There are also other options available under History in Preferences as well, including the number of results and event log entries to be stored.
Figure 4. The Result History Window
The Event Log History window will store and display the event logs and the Result History window will store and display the results from the previous executions of any scripts that have been run. In addition, the script code that produced that result or event log is automatically stored and may be displayed at any time by double clicking on an entry in one of these windows, or by clicking the Show Script button in the window's toolbar. If the code has been modified since the entry was created, a new Script Editor window will be opened, which will contain the original code that was used to generate the entry!
Figure 5. The Event Log History Window
The Event Log History and Result History windows are reset each time that the Script Editor is launched.
Figure 6. The Library Palette
In past versions of Mac OS X, opening application and scripting addition dictionaries in the Script Editor sometimes proved to be a time consuming process. First, you had to select Open Dictionary from the File menu. Then, after a delay, a window would prompt you to select a scriptable application on your machine. The ability to open dictionaries in this manner still exists in the new Script Editor. However, those who prefer an easier way may want to check out the new Library palette, which can be displayed by selecting Library from the Window menu.
By default, the Library palette displays a list of several commonly accessed scriptable applications and scripting additions installed on your machine. Additional applications and scripting additions may be manually added to the list by dragging and dropping them from the Finder directly into the palette. You may also add them by clicking the + button in the window's toolbar, which you will prompt you to select an application or scripting addition.
To quickly open the dictionary for an application in the list, double click on it, or click on the dictionary icon in the window's toolbar.
Clicking the script icon in the toolbar of the Library palette will create a new Script Editor document containing a tell block for the selected application. For example, clicking the script icon after selecting the Finder would generate a new window containing the following code:
tell application "Finder"
New Dictionary Layout
Figure 7. The Finder Dictionary Window
When opening application or scripting addition dictionaries, you will notice some changes as well. Primarily, the list of application terminology on the left side of the window is now organized into groups, also referred to as suites. Each group may be expanded to display collapsible lists of classes, or objects and properties, and commands.
New Editing Features
Let's begin looking at some of the new script editing features of the Script Editor, which are designed to save you time and help you to develop your scripts more efficiently.
Find And Replace
Finally, at long last, probably one of the most requested features of all time has been added to the Script Editor application - the all important Find and Replace! To display the find and replace window, type command + F, or select Find from the Edit > Find menu. Once you begin using this feature, you will wonder how you ever worked without it. It is a tremendous time saver when writing code. In addition, dictionary windows are also now fully searchable, which proves to be quite valuable when looking for a specific term.
Figure 8. The Script Assistant
If you have used Xcode, or other professional development environments in the past, you may be familiar with the term "code completion." This is another great feature that has been built into the new Script Editor.
In order to enable code completion, select Preferences from the Script Editor menu, and click on the Editing icon in the preference window toolbar. Next, select the Use Script Assistant checkbox. You will need to re-launch the Script Editor for the change to take effect.
Once enabled, the script assistant monitors your code as you type and attempts to help you auto-complete the code whenever possible. You will know when code completion is possible because you will see three dots (...) appear after the text you are typing. To auto complete the code, press the F5 key. This will display a list of the suggested AppleScript terms to use. To select a term, use the up/down arrow keys to select the desired term, and then press Enter.
New and Exciting Save Options
In previous versions of the Script Editor, there were three main ways to save your scripts - as Applications, as Compiled Scripts, or in Text Format. In addition to allowing you to save in these familiar formats, the new Script Editor also offers two new options - Script Bundles and Application Bundles!
It is important to note that scripts saved as bundles will not function on systems prior to Mac OS X Panther (10.3). When saving as a bundle, your script is actually saved as a folder, which has the appearance of a single file to the user. A saved bundle contains sub-folders and all resources of your script.
Figure 9. A Script Bundle's Folder Structure
To view and edit the resources of a bundled script, control click on the script in the Finder, and select Show Package Contents from the contextual menu that is displayed. If desired, you may add additional resources to the bundle, including images, icons, script libraries, and more.
Bundles will even allow you to embed scripting additions directly into your script, eliminating the need to install those scripting additions on machines that will run the script. To embed a scripting addition into a script bundle, create a folder named Scripting Additions in the Contents > Resources folder in the bundle, and install the scripting addition into this folder. Please note that the embedded scripting addition must actually be installed on the machine that creates the script bundle.
Ironically, older versions of the Script Editor lacked an important feature, AppleScript support. In other words, they weren't scriptable. The new Script Editor is now fully scriptable, allowing you to begin automating even the creation of your scripts with AppleScript.
Figure 10. Contextual Menus
In addition to being scriptable, the new Script Editor is also attachable, and allows you to trigger scripts from within a contextual menu. To view the Script Editor's contextual menu, control click anywhere in the code of your script. You will notice that Apple has included quite a number of pre-built scripts for you to use, which can do things like add error protection around the selected code in the current Script Editor document.
If desired, you can also add your own scripts into the contextual menu. To do so, select "Open Scripts Folder" from the contextual menu, and add your script(s) into the folder structure. In addition to being displayed in the contextual menu, the scripts in this folder are also displayed in the system-wide Script Menu, which may be enabled by launching the Install Script Menu application, located in the Applications > AppleScript folder.
Since the Script Editor is now scriptable, Apple has also added some interesting new AppleScript features into the Services menu. The Services menu can typically be found in the main menu of most applications in Mac OS X. New AppleScript features in the Services menu include the ability to select text in a document and...
- Run it as an AppleScript
- Open a new Script Editor document containing the text
- Run it as an AppleScript and replace the selected text with the result
For those who are interested in sharing their code on the Internet, Apple has also introduced URL Protocol support into the new script editor. In other words, users can embed AppleScript code in web pages as links. Then, clicking those links will cause the Script Editor to perform a specific action with the embedded AppleScript code. Links can be configured to:
- Create a new Script Editor document containing the embedded AppleScript code
- Insert the embedded AppleScript code at the current position in the front Script Editor document
- Add the embedded AppleScript code to the end of the front Script Editor document
We have explored the primary new features of the updated Script Editor in Mac OS X Panther (10.3). For additional information about the new Script Editor application, please visit the Script Editor page on Apple's AppleScript web site at http://www.apple.com/applescript/scripteditor/.
There are still many other new and exciting AppleScript changes in Mac OS X Panther to explore. In fact, the release of Panther brought with it probably the largest number of AppleScript features and changes since AppleScript was first introduced. In the coming months, we will touch on some of these new AppleScript-related features in Mac OS X. In addition, I will begin to provide you with information designed to teach you the basics of AppleScripting, so that you can put yourself on the way to becoming an expert scripter. Until next time, keep scripting!
Benjamin Waldie is president of Automated Workflows, LLC, a firm specializing in AppleScript and workflow automation consulting. In addition to his role as a consultant, Benjamin is an evangelist of AppleScript, and can frequently be seen presenting at Macintosh User Groups, Seybold Seminars, and MacWorld. For additional information about Benjamin, please visit http://www.automatedworkflows.com, or email Benjamin at firstname.lastname@example.org.