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

Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »
Live, Playdate, Live! – The TouchArcade...
In this week’s episode of The TouchArcade Show we kick things off by talking about all the games I splurged on during the recent Playdate Catalog one-year anniversary sale, including the new Lucas Pope jam Mars After Midnight. We haven’t played any... | Read more »

Price Scanner via MacPrices.net

Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more
New promo at Visible: Buy a new iPhone, get $...
Switch to Visible, and buy a new iPhone, and Visible will take $10 off their monthly Visible+ service for 24 months. Visible+ is normally $45 per month. With this promotion, the cost of Visible+ is... Read more
B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for $100 off Apple’s new MSRP, only $899. Free 1-2 day delivery is available to most US addresses. Their... Read more
Take advantage of Apple’s steep discounts on...
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

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply 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
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
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
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.