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

Web Topics
All Articles
Scripting Languages

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab

JavaScript and Mac OS
Pages: 1, 2, 3

Now you're ready to write your first JavaScript. Type the following text into a Script Editor application window and click "Run":

Core.message("Hello World!");

If an alert box with the words "Hello World!" appear, it worked!

If you get an error, make sure you have "JavaScript" selected in the scripting language pop-up menu and that you typed the command in exactly as specified. JavaScript is case-sensitive, so "Core" and "core" are different names and only "Core" will work.

Don't worry if a result window appears with the word "undefined" in it. Script Editor automatically displays the value returned from scripts, and this sample script does not return any results.

Screenshot of a successful Hello World test.
If your Hello World test works properly, you should see something like this.

If you've used JavaScript in web browsers before, you may be wondering where "Core" comes from and why window.alert("Hello World!") won't work. Neither "Core" nor "window" are part of the JavaScript language -- they are objects supplied by the scripted application to JavaScript.

Related Reading

AppleScript in a NutshellAppleScript in a Nutshell
By Bruce W. Perry
Table of Contents
Sample Chapter
Full Description

Most web browsers, including Netscape Navigator and Microsoft Internet Explorer, use JavaScript internally and do not supply their internal objects to OSA components. The window.alert method is internal to the web browser and not available to OSA components.

The JavaScript OSA component provides some basic functionality in the Core and Mac OS objects for all applications. The methods and properties available in these objects are documented at the Late Night Software JavaScript OSA pages. However, the most useful objects for scripting are supplied by applications and not the JavaScript component.

You can find what scripting objects an application supplies by opening the application's scripting dictionary from Apple's Script Editor application. Scripting dictionaries contain AppleScript terminology, and there are some significant differences in JavaScript terminology:

  • JavaScript does not allow spaces in names. The JavaScript OSA component converts spaces in application dictionaries to underscore characters (_), so you would type the name "startup disk" in the Finder dictionary as startup_disk.

  • The JavaScript OSA component uses arrays where AppleScript uses lists. You can create array literals in JavaScript by enclosing the items in bracket characters and separating them with commas. For example, the AppleScript list {1,2} is represented as [1,2] in JavaScript. Note that in AppleScript the first item of a list is item 1, while in JavaScript the first item has array index 0.

  • AppleScript has the notion of a "result" object which contains the last value or object used. JavaScript does not have such an object, if you want to use a calculated value in another statement, you must assign it to a variable.

  • AppleScript allows referring to inherited objects directly while JavaScript requires specifying the entire hierarchy to the object. For example, in AppleScript you can refer to the startup disk of the desktop object directly in Finder, while in JavaScript, you must use desktop.startup_disk since the startup disk is a property of the desktop object.

With this in mind, we can start to write JavaScript for scriptable Mac OS applications. Finder is a good application to experiment with since it does a lot of useful things. Remember the Wild Hard Drive Mac Hack that had a simple AppleScript to move the startup disk icon? The (slightly modified) AppleScript version is:

   tell application "Finder"
       select startup disk
       set position of selection to {630, 79}
   end tell

We can do the same thing in JavaScript:

   var finder = MacOS.finder();
   finder.desktop.startup_disk.position = [630, 79];

Pages: 1, 2, 3

Next Pagearrow