TweetFollow Us on Twitter

The "NEW" Script Editor

Volume Number: 20 (2004)
Issue Number: 4
Column Tag: Programming

AppleScript Essentials

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.

Library Palette


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"
   
end tell

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.

Script Assistant


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.

AppleScript Support

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

In Closing

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 applescriptguru@mac.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.