Archive for January, 2009

JSFLInterface

Saturday, January 10th, 2009

It won't be long after you start making your first windowSWF (or "Flash Panel") that you'll find yourself needing it to talk to JSFL. After all, JSFL is what's really doing the heavy lifting behind the scenes of that beautiful Flash interface you've created. This communication is accomplished with the MMExecute function. MMExecute accepts a string of (escaped) JavaScript and executes it. Simple right? Well, if you've made your way here, you probably already know that this can get to be a pain pretty quickly. Adding backslashes, changing double quotes to single quotes, concatenating strings, serializing objects… it gets to be a mess. Several people have come up with solutions to avoid the hassle but, being the egotist I am, I think mine is the simplest.

You can get the JSFLInterface class from my subversion repository.

If you've ever used Adobe's ExternalInterface, you'll be instantly at home with the JSFLInterface. Simply import the class and use its call method to make JSFL calls. Like ExternalInterface.call, the first argument is the function name and subsequent arguments are passed to your JavaScript function. Your arguments will automatically be serialized. Your JSFL function can even return values (including Objects, Arrays, Dates, and the "primitive" types), which will be automatically converted into ActionScript objects, using some of the same code as ExternalInterface.

I think it's pretty self-explanatory but here's an example anyway:

import com.exanimo.external.JSFLInterface;
JSFLInterface.call('fl.trace', 'Hello World'); // traces "Hello World"
if (JSFLInterface.call('fl.fileExists', 'file:///C|/example.fla') == true)
{
	JSFLInterface.call('fl.trace', 'file exists!');
}
else
{
	JSFLInterface.call('fl.trace', 'file not found!');
}

As usual, this class is available under the MIT License, with the additional request that any improvements you make be returned here so we can all benefit from them.