HomeThinkDifferentStoreMacBoardsAdvertisingRSS SyndicationNewsletterContact

[Moore's Views & Reviews] If You Haven't Tried AppleScript, You're Missing Something Insanely Great

Thursday, March 16, 2000


By Applelinks Contributing Editor Charles W. Moore

Regular Applelinks readers are aware that I'm a big fan of Tom Bender's powerful little text editor, Tex Edit Plus, which is my number one, do-almost-everything application. One of the reasons Tex Edit Plus is so versatile is its comprehensive support of AppleScript, Apple's English-like programming language that allows you to use Apple Events to control applications.

As Tom Bender puts it: "AppleScript puts the power of these Apple Events in the hands of the ordinary user. It’s just another insanely great advantage that we Mac users enjoy." Indeed. Tex Edit is one reason why I thank myself every day for choosing a Mac. Now, I've discovered, AppleScript is another.

AppleScript can be used to write script files which can control the actions of the computer and applications that run on it. Much more than just a macro-language that simply repeats your recorded actions, Apple says that AppleScript scripts can "think." Scripts can make decisions based on user-interaction or by parsing and analyzing data, documents or situations. AppleScript can be employed to create shortcuts for automating tasks like naming files, resetting preferences, or connecting to the Internet.

What’s an Apple Event? Since System 7, The MacOS has had a cool protocol that permits applications to communicate with each other. Each Apple Event represents a single “message” in this digital dialog. For instance, when you double-click on a document icon, the Finder sends an "open document" Apple Event to the appropriate application, which then recognizes the event and displayed this document for you.

Apple Events also allow "scriptable" applications to commune with themselves internally and efficiently, which allows a substantial amount of function customization without altering the basic program coding.

For example, one of the things I use Tex Edit Plus for every day is creating HTML documents of articles for posting on the Applelinks news page. The article you're reading was written and "HTMLed" using Tex Edit Plus and several AppleScripts. Unlike, say, BBEdit, Tex Edit Plus has no built-in HTML editor, but several pre-fabricated AppleScripts are available that perform various HTML conversion functions. The script I use for basic text to HTML conversions as written by Bill Cheeseman, and it does a very quick, clean job, including conversion of style data like font sizes, color, and so on to HTML. I use another AppleScript that I downloaded from Doug Adams’ online Tex Edit AppleScript archive for creating Web links.

One of the very cool things about AppleScript is that Apple includes a little application called ScriptEditor with the MacOS, and with it you can create your very own AppleScripts from scratch, or customize existing ones to do the job you want.

For example, one HTML conversion task I was not able to find a pre-fab AppleScript for is converting paragraph tags to line break tags. Bill Cheeseman's converter only creates paragraph breaks. This meant that I either had to type in the break tags manually, or switch to another program such as BBEdit if there were a lot of break tags to insert.

The solution? Write an AppleScript of my own for converting paragraph tags to line break tags. Now, while I possess a number of talents, programming is not one of them. I can manage basic HTML, although I usually let an application do the work of conversion, and I fooled around a bit with scripting in HyperCard years ago, but writing code -- even really simple code -- is not my bag. I am terrible with tasks that require attention to painstaking detail, which is why I'm semi-innumerate, and I'm absent-minded to boot.

Tex-Edit is "recordable" as well as "scriptable," and you can use Script Editor to record your actions as you use Tex-Edit, creating a script that you can save and use to repeat the action later by activating it with a menu command.

Faced with a real, practical reason to learn a bit of AppleScript, this week, finally, after all these years, I started up the AppleScript Script Editor. The fact that it took me about 20 minutes to create the desired (albeit extremely simple) script -- one-handed -- while talking on the telephone about something entirely different -- says volumes about how easy AppleScript is to use even for complete programming doofuses like me.

After a couple of false starts, using the Script Editor's "Record" function, and some borrowing and minor hacking of stuff from other scripts, I had my little script, which I proudly named "Break." It looks like this:

Kindergarten stuff for real programmers I'm sure, but what a great feeling of accomplishment for me when I dropped "Break" into the Tex Edit Plus Scripts Folder, started the program, and the thing actually worked!

Bursting with new-found confidence, I went back to the Script Editor and hacked out another little script to insert a break tag after selected text. I discovered that for both these particular tasks, the "Record" function was not capable of doing what I needed done, but by looking at other, pre-recorded AppleScripts, I was able to figure out the necessary syntax. My second AppleScript, "Insert Break," looks like this:

My appetite is whetted, and I expect I'll be experimenting with more AppleScripting now that I've got my feet wet.

A Scriptable application is one in which the programmer has given AppleScript access to major portions of the program’s inner workings. There are many AppleScriptable Mac applications, but I think it is fair to say that no developer has embraced and integrated AppleScript more enthusiastically than Tom Bender with Tex Edit.

When ScriptEditor saves a script as a "compiled script," whether recorded or written from scratch, it creates a script document which can be placed in Tex-Edit’s "Scripts" folder. When Tex-Edit launches, it adds everything in this folder to its Scripts menu, providing easy access to your AppleScript functions.

If you would like to experiment with AppleScripts in Tex Edit and don't want to write or record your own yet, you can visit Doug Adams' http://www.malcolmadams.com/te/ Tex Edit AppleScript archive, which also contains an AppleScript guide for beginners:

The last time I looked, Doug had 139 prefabricated AppleScripts posted for free download and use with Tex-Edit Plus. Script categories include:

• Text & Character
• Document Conversions
• Window Manipulation
• Working With Files
• Printing
• HTML Authoring
• Search, Sort, Replace
• Fun Scripts
• Miscellaneous
• TE+ & Other Apps

You can download the latest version of Tex-Edit Plus 2.9 from:
http://www.nearside.com/trans-tex/

Another text editor with extensive AppleScript support is Marco Piovanelli's Style, which like Tex Edit is scriptable and recordable, with an easily customizable Script menu.

Style is also attachable, which means you can attach scripts to Style, even while it’s running, to modify the way it handles certain commands, like close and save.ion’s logic.

An attachment script can intercept most of the events that Style sends to itself during normal operation, including quit, make new document, open, close and save. In addition, an attachment script can intercept user-defined events sent from external sources. In this case, you must specify the event using its four-letter event class and four-letter event ID rather than its name, because your custom event is not listed in Style’s dictionary.

To edit a script in the Script menu from within Style, hold down the option key while you choose the script from the menu. The script will open in the Script Editor.

Style is $10 shareware with a 30 day trial period. For more information or to download Style, visit:
http://www.merzwaren.com/style/

Popular Mac word processors like MS Word, AppleWorks, and Nisus Writer, as well as many other applications support AppleScript.

Of course, AppleScript is not limited to Tex Edit or Style or word processors or any other particular application program. You can also write an AppleScript to automate Finder functions like emptying the Trash, or adding an alias to the Apple menu.

AppleScript in Mac OS 9

The version of AppleScript bundled with Mac OS 9 includes several new features, more power, and other improvements including:

• Apple events over IP. With Mac OS 9 you can now control any Mac across an IP-based network, even the Internet. Using the new Keychain control panel, all program-linking connections are secure, "dialog free," and can be performed by un-attended scripts. A new AppleScript GuideBook Module is available which describes the process step-by-step including sample scripts.

• Scriptable Voice Recognition. Mac OS 9 introduces the Speech Listener application which can be used to write scripts that respond to spoken words or phrases. Your scripts can now converse with users to gather information and perform actions. Amazing! A new AppleScript GuideBook Module is available which describes how to script all aspects of speech on the Mac.

• Enhanced Sherlock Scriptability. New commands allow the creation and control of channels and sites. Plus... using the new AppleScript over IP abilities of Mac OS 9, you can perform Sherlock searches on other computers over the Internet.

• Enhanced System Profiler Scriptability. The latest version of the Apple System Profiler offers extensive scriptable access to necessary information about your computer including video memory, devices, RAM installed, etc. The new AppleScript can be used over IP to manage multiple machines over a network.

• Scriptable Memory Control Panel. You can write scripts to switch between various virtual memory settings and setting up RAM disks.

You can find detailed AppleScript information, tutorials, scripts, and links to other resources on the AppleScript Web site:
http://www.apple.com/applescript/

The Script Editor can save scripts as stand-alone script applications that run the script when the script application icon is double-clicked, and the ScriptEditor versions that ship with Mac OS 8.6 and later even have an option to save script applications for use on Mac OS X. Script applications saved this way can be used on this Mac OS (or any version of Mac OS back to Mac OS 8.1 with the Carbon extension installed in the Extension folder) or on future versions of Mac OS X.

There are many other sources of useful scripts on the Web as well. Do a Web search for AppleScript. Some other AppleScript resources on the Internet include:
Bill Cheeseman’s AppleScript Sourcebook:
http://www.applescriptsourcebook.com/
and
The Late Night Software:
http://www.latenightsw.com/


Charles W. Moore

  

[an error occurred while processing this directive]

Email This Article - Comment On This Article


 

.

October 12, 2008

My Applelinks

eMail
Weather
Web Tools
MacBoards
Mailing List

Help
Logout
Forgot Password
Privacy
Register

Applelinks Store
Reader Specials
Sherlock Plug-in

 

Hot Topics
.•Functional Neutral,” Quill Mouse Now Listed On GSA Section 508
10/30/2003

Special Report: Coming MS Explorer a Problem for Websites with Active Content
10/27/2003

Spam Is Starting To Hurt Email - New Pew Report
10/24/2003

Reviews
.•Toast 6 Titanium
11/06/2003

Extensis pxl SmartScale
11/04/2003

Super GameHouse Solitaire Collection
10/27/2003

Columns
.•Game On Eileen Part II (or, Hello, Obsidian, how's the wife?)
10/31/2003

Charles Moore Reviews The Encyclopedia Britannica Ultimate Reference Suite 2004 [Link Fixed!]
10/31/2003

Kevin Murphy: Author, Moviegoer, Robot
10/29/2003

Macopinion
.[an error occurred while processing this directive]

MacBoards
.[an error occurred while processing this directive]