Jump to content


Photo

javascript to flash panel communication


  • Please log in to reply
5 replies to this topic

#1 Jeff Young

Jeff Young

    FireworksCommander

  • Members
  • Pip
  • 7 posts

Posted 23 April 2011 - 09:33 PM

For some reason I can't work out how to pass information from the Fireworks javascript API back to my actionscript code that is used to define my flash panel.

For example, if I want to pass an array of page names from the javascript environment back to my flash panel, and then use that array as a dataprovider for a combobox.

MMExecute goes from actionscript -> javascript
What goes the other direction?

#2 Jeff Young

Jeff Young

    FireworksCommander

  • Members
  • Pip
  • 7 posts

Posted 23 April 2011 - 11:03 PM

Ok, after tearing my hair out for a while it occurred to me to stop trusting the extending fireworks pdf, which says:

MMExecute()
Usage
MMExecute(jsToPass)
Arguments
jsToPass
A string of JavaScript for Fireworks to execute.
Returns
Nothing.


Er, actually, it appears to return a string - which is quite useful really.

Found it on this page http://www.adobe.com...panels_pt2.html

MMExecute doesn't just execute a command string; it also returns the result of that command back to Flash.



#3 jdunning

jdunning

    Fireworks Ninja

  • Members
  • PipPipPipPipPip
  • 173 posts

Posted 24 April 2011 - 02:53 PM

Ok, after tearing my hair out for a while it occurred to me to stop trusting the extending fireworks pdf, which says:

Sadly, it's not 100% trustworthy.

The best way I've found to communicate from Flash to the JS API is described in this cookbook article:
http://cookbooks.ado...h_pa-16399.html

Since global JS variables stick around as long as FW is running, a panel can load a big chunk of JS that defines a bunch of methods that hang off a global variable. The panel can then make calls to the API as needed. That's much easier than building up long strings of JS code to pass to MMExecute calls.

You can also encapsulate MMExecute in another function that uses a JSON library to convert its arguments to a valid JS string. Then you can make calls to your API like this:

callMethod("MyPanelAPI.setVisibility", false, ["Layer1", "Layer2", "Background"]);

which gets converted to a MMExecute call like this:

MMExecute('MyPanelAPI.setVisibility(false, ["Layer1", "Layer2", "Background"])');

If you need to return something more complex than a string from JS to your panel, your JS code can return JSON, and then your AS3 code can use a JSON library to turn that back into a live object. The toSource() method that's built into the FW JS interpreter can be handy for turning data into a string of JS. (It's not true JSON, though, as it doesn't quote the object properties, so some libraries make complain.)

#4 jdunning

jdunning

    Fireworks Ninja

  • Members
  • PipPipPipPipPip
  • 173 posts

Posted 24 April 2011 - 03:02 PM

You may also find the Fireworks Console panel helpful when developing Flash panels: http://johndunning.com/fireworks/about/FWConsole

It comes with a .swc library that you can add to your Flash project. You then import com.johndunning.fw.console, which lets you make calls to console.log(). The string representations of the values you pass to that method will get displayed in the Fireworks Console panel. This makes it much easier to see what's going on while your panel is running.

#5 Jeff Young

Jeff Young

    FireworksCommander

  • Members
  • Pip
  • 7 posts

Posted 24 April 2011 - 05:45 PM

Thanks for that,

Yes, have been using that cookbook article to create a js API - very useful.

Didn't know about the toSource() method - will give it a go on some JSON.

Am using console :)

Having a few problems with scriptrunner, stopped working a while back, isn't listing results right now

#6 jdunning

jdunning

    Fireworks Ninja

  • Members
  • PipPipPipPipPip
  • 173 posts

Posted 25 April 2011 - 07:45 AM

Didn't know about the toSource() method - will give it a go on some JSON.

Having a few problems with scriptrunner, stopped working a while back, isn't listing results right now

I've been using as3corelib to handle JSON on the AS3 side: https://github.com/m...bers/as3corelib

For the Script Runner panel, you could try deleting the Code.txt and Output.txt files in \Command Panels\Script Runner\. Could be that they've gotten too big, or something about them is confusing the panel.