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.


Comments

Gravatar Aaron Smith

Nice work man. I've got a similar interface here: http://codeendeavor.com/gsdocs/net/guttershark/util/JSFLProxy.html

Gravatar matthew

Seems like we're doing some similar work (: But what happens if you want to execute one of the built-in methods, like fl.fileExists in the example above?

Gravatar George Profenza

Hi,

I gave jsflInterface a try. Calling functions is no problem. Does the same apply when you create variables ? Also, when I call a jsfl method from a WindowSWF, nothing gets recorded in the History Panel, so no UNDO :(

Any way to get around that ?

Gravatar David

Really nice! Works like a charm.

One thing I would add is the ability to get properties. Like fl.configURI and so on. You've already done the hard work and this would make it a more complete JSFL interface. Regardless, thanks for sharing. This is a huge time saver!

Gravatar matthew

@David The Flash JS environment defines a real eval function, so you can run any arbitrary JavaScript, and JSFLInterface will return the result. In your case, try something like JSFLInterface.call("eval", "fl.configURI"). Do you think there's a need to add a "getProperty"-type method? IMO, there's no need to complicate the interface, and if you're looking for something shorter, you can always do MMExecute("fl.configURI"). Let me know if you disagree.

@George This same technique can be used to create variables. However, it seems to me that when you're doing that, it's usually a better idea to externalize the script.

As for the History Panel, I don't know if it's possible to manipulate that or not. If you find something on the subject, please let me know and I'll update the class.

Gravatar David

Ahh, didn't think about calling eval, which I think is cleaner than running MMExecute directly. While it may complicate it a little I like the idea of the interface handling properties and methods. But I certainly see your point too. You're call. Either way it's helped me immensely and I appreciate what you've done here.

Gravatar Dave Stewart

Hi Matthew,

I'm developing a rather comprehensive JSFL library at the moment, and as you said, your class has come in rather handy! I've made a few alterations to allow it to function as a library rather than the executing class, as well as a few other functional tweaks.

Maybe you'd like to email me directly to discuss what sort off attribution you'd like, and (also as you said above) if you'd like to include any of the changes in your original code.

Cheers,
Dave

Gravatar AS3 JSFL Lib complete « xJSFL

[...] Serialize and deserialize native AS3 / JavaScript objects using the library from Exanimo [...]

Gravatar Rgb

Do you want to sell your domain name?