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

Seven Knights Idle Adventure drafts in a...
Seven Knights Idle Adventure is opening up more stages, passing the 15k mark, and players may find themselves in need of more help to clear these higher stages. Well, the cavalry has arrived with the introduction of the Legendary Hero Iris, as... | Read more »
AFK Arena celebrates five years of 100 m...
Lilith Games is quite the behemoth when it comes to mobile games, with Rise of Kingdom and Dislyte firmly planting them as a bit name. Also up there is AFK Arena, which is celebrating a double whammy of its 5th anniversary, as well as blazing past... | Read more »
Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »

Price Scanner via MacPrices.net

Apple introduces the new M4-powered 11-inch a...
Today, Apple revealed the new 2024 M4 iPad Pro series, boasting a surprisingly thin and light design that pushes the boundaries of portability and performance. Offered in silver and space black... Read more
Apple introduces the new 2024 11-inch and 13-...
Apple has unveiled the revamped 11-inch and brand-new 13-inch iPad Air models, upgraded with the M2 chip. Marking the first time it’s offered in two sizes, the 11-inch iPad Air retains its super-... Read more
Apple discontinues 9th-gen iPad, drops prices...
With today’s introduction of the new 2024 iPad Airs and iPad Pros, Apple has (finally) discontinued the older 9th-generation iPad with a home button. In response, they also dropped prices on 10th-... Read more
Apple AirPods on sale for record-low prices t...
Best Buy has Apple AirPods on sale for record-low prices today starting at only $79. Buy online and choose free shipping or free local store pickup (if available). Sale price for online orders only,... Read more
13-inch M3 MacBook Airs on sale for $100 off...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices, along with Amazon’s, are the lowest currently available for new 13″... Read more
Amazon is offering a $100 discount on every 1...
Amazon has every configuration and color of Apple’s 13″ M3 MacBook Air on sale for $100 off MSRP, now starting at $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD): $999 $100 off... Read more
Sunday Sale: Take $150 off every 15-inch M3 M...
Amazon is now offering a $150 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 15″ M3 MacBook... Read more
Apple’s 24-inch M3 iMacs are on sale for $150...
Amazon is offering a $150 discount on Apple’s new M3-powered 24″ iMacs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 24″ M3 iMac/8-core GPU/8GB/256GB: $1149.99, $150 off... Read more
Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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.