Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript
Megnut

Web Topics
All Articles
Browsers
ColdFusion
CSS
Database
Flash
Graphics
HTML/XHTML/DHTML
Scripting Languages
Tools
Weblogs

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab






JavaScript and Mac OS
Pages: 1, 2, 3

JavaScript applets

It's not that useful to convert working AppleScript to JavaScript, but it is useful to convert working JavaScript on another platform to run on Mac OS. Chapter 1 of O'Reilly's book, JavaScript: The Definitive Guide, includes the following JavaScript written for a web browser:



   document.write("<h2>Table of Factorials</h2>");
   for(i = 1, fact = 1; i < 10; i++, fact *= i) {
       document.write(i + "! = " + fact);
       document.write("<br>");
   }

The Mac OS does not have a built-in document object, so we will use the JavaScript OSA's Core object. The converted script is:

   var s = "Table of Factorials\n";
   for(i = 1, fact = 1; i < 10; i++, fact *= i) {
       s += "\n" + i + "! = " + fact;
   }
   Core.message(s);

Running this script from Script Editor gave the same results as running the original script in Netscape Navigator, Internet Explorer, or iCab.

You can save JavaScripts as standalone applets with Script Editor; just save the script as a "classic applet" for Mac OS 8 or 9, or as a Mac OS X applet for Mac OS X. Anyone can run applets; you don't need Script Editor or need to know JavaScript.

Porting JavaScript from other platforms

If you want to use the same JavaScript on different platforms, it's useful to create wrapper objects that work the same on each platform. Rather than changing all occurrences of window.alert to Core.message in your browser scripts, you could define an object literal named window with a property named alert that contains a function literal which calls Core.message in the JavaScript OSA with the same argument passed to the property.

var window = {alert: function(s) {Core.message(s)} };

If you haven't used literals before this may look confusing. All it does is allow you to call:

window.alert("Hello World!");

in your JavaScript code, and have the Mac OS JavaScript OSA call Core.message. The same principle can be used to override any application objects in your existing JavaScript code.

Related Reading

JavaScript: The Definitive Guide, 4th EditionJavaScript: The Definitive Guide, 4th Edition
By David Flanagan
Table of Contents
Index
Sample Chapter
Full Description

If you're interested in exploring the JavaScript OSA further, take a look at some of the files in the Sample Scripts folder that comes with JavaScript for OSA. This folder contains sample application scripts, web CGIs, applets, and examples of using the Core and Mac OS objects.

JavaScript drawbacks

So if you can do everything in JavaScript that you could in AppleScript, why would you ever want to use AppleScript? Unfortunately, JavaScript has a few drawbacks for scripting Mac OS applications:

  • JavaScript often takes more memory than AppleScript. This most often manifests itself as an "out of memory" or "error -108" message while running a script. To run the script, you will have to quit the application, increase its Preferred Size in the Memory Requirements section of the application's Info window in Finder, and run the script again. I increased the Preferred Memory of the Script Editor application to 3,000 K. You will also need to increase the preferred size of any JavaScript applets you create.

  • JavaScript is not recordable. You will not be able to create JavaScript from an application by clicking the "Record" button in the Script Editor.

  • There are a few bugs in using application objects from JavaScript. You may have noticed in the Wild Hard Drive example, I converted an AppleScript statement that set the position of a selection, to a JavaScript statement that set the position of the startup disk. Why didn't I use the JavaScript object finder.selection[0]? It seems there is a bug in the JavaScript OSA component in returning properties of the startup disk from a selection. Properties of other files, even other disks, will work, but the property of the startup disk does not. You may run into similar problems with other application objects.

JavaScript is an interesting language that has enormous potential on Mac OS. Aside from providing an alternate method of scripting existing applications, it provides a way to create your own cross-platform Macintosh applications.

Richard Hough is a web developer for a Vancouver, Canada educational software company.


Return to the JavaScript and CSS DevCenter.