|
|
JavaScript and Mac OS Now you're ready to write your first JavaScript. Type the following text into a Script Editor application window and click "Run":
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.
If you've used JavaScript in web browsers before, you may be wondering where "Core" comes from and why
Most web browsers, including Netscape Navigator and Microsoft Internet Explorer, use JavaScript internally and do not supply their internal objects to OSA components. The 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:
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:
We can do the same thing in JavaScript:
|
|
|
|
|
||||||||