|
Flash CS4 Resources |
Working with the JavaScript APIThe Flash JavaScript API lets you write scripts to perform several actions in the Flash authoring environment (that is, while a user has the Flash program open). This functionality is different from the ActionScript language, which lets you write scripts to perform actions in the Flash Player environment (that is, while a SWF file is playing). This functionality is also different from JavaScript commands that you might use in pages displayed in a web browser. Using the JavaScript API, you can write Flash application scripts to help streamline the authoring process. For example, you can write scripts to automate repetitive tasks or add custom tools to the Tools panel. The Flash JavaScript API is designed to resemble the Adobe® Dreamweaver® and Adobe® Fireworks® JavaScript API (which were designed based on the Netscape JavaScript API). The Flash JavaScript API is based on a Document Object Model (DOM), which allows Flash documents to be accessed using JavaScript objects. The Flash JavaScript API includes all elements of the Netscape JavaScript API, plus the Flash DOM. These added objects and their methods and properties are described in this document. You can use any of the elements of the native JavaScript language in a Flash script, but only elements that make sense in the context of a Flash document have an effect. The JavaScript API also contains methods that let you implement extensibility using a combination of JavaScript and custom C code. For more information, see C-Level Extensibility. The JavaScript interpreter in Flash is the Mozilla SpiderMonkey engine, version 1.5, which is available on the web at www.mozilla.org/js/spidermonkey/. SpiderMonkey is one of the two reference implementations of the JavaScript language developed by Mozilla.org. It is the same engine that is embedded in the Mozilla browser. SpiderMonkey implements the core JavaScript language as defined in the ECMAScript (ECMA-262) edition 3 language specification and it is fully compliant with the specification. Only the browser-specific host objects, which are not part of the ECMA-262 specification, are not supported. Similarly, many JavaScript reference guides distinguish between core JavaScript and client-side (browser-related) JavaScript. Only core JavaScript applies to the Flash JavaScript interpreter. Creating JSFL filesYou can use Adobe Flash CS4 Professional or your preferred text editor to write and edit Flash JavaScript (JSFL) files. If you use Flash, these files have a .jsfl extension by default. To write a script, select File > New > Flash JavaScript File. You can also create a JSFL file by selecting commands in the History panel. Then click the Save button in the History panel or select Save As Command from the panel menu. The command (JSFL) file is saved in the Commands folder (see Saving JSFL files). You can then open the file and edit it the same as any other script file. The History panel provides some other useful options as well. You can copy selected commands to the Clipboard, and you can view JavaScript commands that are generated while you are working in Flash. Saving JSFL filesYou can have JSFL scripts available within the Flash authoring environment by storing them in one of several folders within the Configuration folder. By default, the Configuration folder is in the following location:
To determine the location of the Configuration folder, use fl.configDirectory or fl.configURI, as shown in the following example: // store directory to a variable var configDir = fl.configDirectory; // display directory in the Output panel fl.trace(fl.configDirectory); Within the Configuration folder, the following folders can contain scripts that you can access in the authoring environment: Behaviors (to support the user interface for behaviors); Commands (for scripts that appear on the Commands menu); JavaScript (for scripts used by Script Assist to populate the user interface controls); Tools (for extensible tools in the Tools panel); and WindowSWF (for panels that appear in the Windows menu). This document focuses on scripts used for commands and tools. If you edit a script in the Commands folder, the new script is immediately available in Flash. If you edit a script for an extensible tool, close and restart Flash, or else use the fl.reloadTools() command. However, if you used a script to add an extensible tool to the Tools panel and you then edit the script, either remove and then add the tool to the Tools panel again, or else close and restart Flash for the revised tool to be available. There are two locations where you can store command and tool files so they can be accessed in the authoring environment.
If a JSFL file has other files that go with it, such as XML files, store them in the same directory as the JSFL file. Running scriptsThere are several ways to run scripts. The most common ways are explained in this section. To run a script that you are currently viewing or editing:
This option lets you run a script before you have saved it. This option also lets you run a script even if no FLA files are open. To run a script that is in the Commands folder, do one of the following:
To run a command script that is not in the Commands folder, do one of the following:
To add a tool implemented in a JSFL file to the Tools panel:
You can add individual JavaScript API commands to ActionScript files by using the MMExecute() function, which is documented in the ActionScript 3.0 Language and Components Reference. However, the MMExecute() function has an effect only when it is used in the context of a custom user interface element, such as a component Property inspector, or a SWF panel within the authoring environment. Even if called from ActionScript, JavaScript API commands have no effect in Flash Player or outside the authoring environment. To issue a command from an ActionScript script:
To run a script from the command line on Windows:
To run a script from the “Terminal” application on the Macintosh:
|