n this article I will discuss the similarities and differences between ActionScript and JSFL. We will also briefly show you how to write your own JSFL scripts and how to execute them.As with ActionScript, JSFL has a set of predefined classes, containing methods and properties:
ActionScript coders will notice one extra class, the RegExp class. No, that isn’t a mistake; you can use regular expressions in JSFL!
In JSFL, as in ActionScript, every string is an instance of the String class, every array is an instance of the Array class, every number is an instance of the Number class, etc. Which means that strings, numbers, arrays, booleans, objects, etc., are treated in exactly the same way as they are in ActionScript
All Strings have the usual String methods: ‘substr,’ ‘charAt,’ ‘indexOf,’ etc. All Arrays have the usual Array methods: ‘splice,’ ‘pop,’ ‘push,’ and so on.
So for example, you can write:
myStr="A,B,C,D"; bits=myStr.split(","); //use a string method
myStr="abcd"; myStrLen=myStr.length; //get a string property
Top-Level Properties and MethodsJSFL also contains some top-level functions. These are functions that are not related to any particular object or class and thus can be used anywhere in your scripts, as is.
Writing a JSFL Script
You create a JSFL script like you would an external ActionScript file. Using your favorite script editor, you write your JSFL code, then you save the file with a .jsfl extension. You can call the file whatever you like as long as it has the correct file extension.
You can also open JSFL scripts for editing in Flash MX 2004. Naturally your JSFL script will automatically open for editing in the JSFL Script Editor. To edit a JSFL script in Flash MX 2004, open the File menu and then select Open. From there, navigate through your local machine to find the correct JSFL script to edit.
|Figure 2. Purpose: Rollover a particular method or property for a brief explanation of its purpose.
As with the ActionScript Editor, the JSFL Script Editor also contains a list of every object and each object’s available methods and properties along the left-hand side. This does come in very handy, as there are a lot of them to remember. Rollover a particular method or property on the left-hand side and you will get a tool tip which will briefly explain what that particular method or property does. The JSFL Script Editor also has syntax highlighting and code hinting.
Executing a JSFL Script
To execute a JSFL script, the Flash MX 2004 IDE must be open. The simplest form of a Flash extension, the Command, has its own special place in the Flash MX 2004 IDE. That is the Commands menu:
Figure 3. The Commands Menu: Run Command from the Commands menu to execute your scripts.
Figure 4. My JSFL Scripts: The Commands menu will show your personal JSFL scripts. Select a name to execute the script.
When you select Run Command from the Command menu, you can locate a JSFL script to execute. The Commands menu displays three default options, shown in Figure 3, but it also adds a new option for each JSFL script. This new option is contained within a special Commands directory in the Flash MX 2004 Configuration folder. The location differs with different operating systems:
Windows XP or Windows 2000:C:Documents and Settings
Local SettingsApplication DataMacromediaFlash MX 2004 ConfigurationCommandsWindows 98:C:WindowsApplication DataMacromediaFlash MX 2004 ConfigurationCommandsMacintosh 0SX:Hard Drive/Users/ /Library/Application Support/Macromedia/Flash MX 2004/ /Configuration/Commands/
So, if you place your JSFL scripts in this directory, you can execute them directly from the Commands menu, by selecting the name of your script file from the list of options.
As shown in Figure 4, I presently have four JSFL scripts located in my Configuration/Commands directory, with the following filenames:
- Batch Run.jsfl
- Save Copy As.jsfl
When a JSFL script is executed all functions that are called update the state of a particular element in a Flash document immediately, which differs from ActionScript execution. For example in ActionScript, if you move a Movieclip across the stage in a for loop, you would only see the final state of the loop, as the stage is not updated until the ActionScript has been executed. However, if you were to call the ‘deleteLayer’ function of a timeline object in JSFL inside of a for loop, the layer is immediately deleted and the authoring environment will refresh to display the new state of the timeline before the code after the for loop is executed.