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

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 »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »

Price Scanner via MacPrices.net

Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more
Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more

Jobs Board

Licensed Practical Nurse - Womens Imaging *A...
Licensed Practical Nurse - Womens Imaging Apple Hill - PRN Location: York Hospital, York, PA Schedule: PRN/Per Diem Sign-On Bonus Eligible Remote/Hybrid Regular Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.