JSFL: Export layers to SWF

I wanted a way for the art team to use one FLA, which works best for asset reuse and their workflow, but have many SWF files, which was the most flexible deploy method for our eng, team.

I found the excellent following post on Lost in Actionscript:

http://lostinactionscript.com/2009/04/03/export-layers-to-swf-with-jsfl/

I changed the script so that you could group layers using timeline folders.  For any layers not in a folder, the layer name will become the name of the SWF.  For layers in a folder, they will be exported into a SWF with the folder name as the SWF name.

Be sure to turn the ‘export in first frame’ option off on your library assets – unless you want them to be in all of the exported SWFs!

/**
*    This script will export each layer as swf using the layer name.  
*    Any layers in a folder will be exported together using the folder name
*/

var doc = fl.getDocumentDOM();
var lyrs = doc.getTimeline().layers;

var len = lyrs.length;
var lyr;
var originalType;
var i;

var saveDir = fl.browseForFolderURL("Choose a folder in which to save your exported SWFs:");
if (saveDir)
{
    fl.outputPanel.clear();

    var originalTypes = new Array();

    for (i=0; i < len; i++) 
    {
        lyr = lyrs[i];
        originalTypes[i] = lyr.layerType;
    }

    for (i=0; i < len; i++)
    {
        lyr = lyrs[i];
        if(lyr.layerType == "normal")
            lyr.layerType = "guide";
    }

    for (i=0; i < len; i++)
    {
        lyr = lyrs[i];
        originalType = originalTypes[i]
        fl.trace(lyr.name);
        if (originalType == "normal" && lyr.parentLayer == null ) 
       {
            lyr.layerType = "normal";
            doc.exportSWF(saveDir+"/"+lyr.name+".swf", true);
            fl.trace("Exported: " + lyr.name+".swf");
            lyr.layerType = "guide";
        }
        else if(originalType == "folder")
        {
            var j = i+1;
            var isChild = true;
            var childLyr;
            while(isChild == true)
            {
                childLyr = lyrs[j];

                if(childLyr != null && childLyr.parentLayer != null)
                {
                    childLyr.layerType = "normal";
                    fl.trace("    "+childLyr.name);
                }
                else
                {
                    isChild = false;
                }
                j++;
            }

            doc.exportSWF(saveDir+"/"+lyr.name+".swf", true);
            fl.trace("Exported: " + lyr.name+".swf");

            j = i+1;
            isChild = true;
            while(isChild == true)
            {
                childLyr = lyrs[j];
                if(childLyr != null && childLyr.parentLayer != null)
                {
                    childLyr.layerType = "guide";
                }
                else
                {
                    isChild = false;
                }

                j++;
            }
        }
    }

    for (i=0; i < len; i++)
    {
        lyr = lyrs[i];
        lyr.layerType = originalTypes[i];
    }
}
Advertisements
JSFL: Export layers to SWF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s