Wednesday, July 18, 2012

JSFL Rocks

I've always loved being able to automate repetitive tasks. Whether using AppleScript with Quark or Lingo with Director in the 1990s, to shell scripting Solaris and OSX, Illustrator with JavaScript, or Python with Blender.

For some reason, I've neglected JSFL with Flash (JavaScript automation for Flash Professional). It's been in the software since 2004. Recently I started looking into it, given the amount of documentation on the subject I think I know why I waited, but now I wish I didn't. The thrill of being able to type a few lines of code to save you time and drudgery is addictive when it hits.

I just had a small but practical real world use of my newfound JSFL skills on a FLA I am building. I'd created 9 symbols and set there base class to flash.display.Sprite as I made each one. After the fact, I decided that the 9 symbols that I'd made sprites should really be flash.display.MovieClip. In less time than it'd take to open up each one's property dialog and reassign its base class, I was able to type run the following in my script:


function changeBase(){
for each( var i in fl.getDocumentDOM().library.items){
i.linkageBaseClass = "flash.display.MovieClip";
}
}
changeBase();

If you've never tried JSFL before, here are some simple steps to get started.
  1. Create a text document with the JSFL extension.
  2. Add your code.
  3. From Flash, select Commands -> Run Command...
  4. Find your JSFL file (from step 1)
Start simple with a file like this, hello.jsfl:
alert("Hello, world!");

Then wrap it in a function:
function hello(){
alert("Hello, world!");
}
hello();

The code outside the function is what actually executes. This makes it easy to add additional functions and test. Eventually, you can create a gui for your code in Flash and create your own tools that appear under Window -> Other Panels, but that's a post for another day. In the meantime, here is some more info from Adobe's site as well as a PDF reference.