Publish Flash layers to single SWFs

In case you need too, this little Flash IDE script will let you publish every single Flash timeline layer as a separate SWF file. Just use unique and URL save layer names and you will get all your SWFs dynamically created as flaFilename + layerName + ".swf".

Just save this little code as a .jsfl file and open it with the Flash IDE:

function filterLayers( timeline, layerName )
{
	var layers = timeline.layers,
		layer,
		c,
		i;
 
	for ( i = layers.length - 1; 0 < i; i-- )
	{
		layer = layers[ i ];
 
		if ( layer && layer.layerType !== "folder" && layer.name !== layerName )
		{
			timeline.deleteLayer( i );
		}
	}
}
 
function publishNewFile( fileURL, layerName )
{
    var document = fl.openDocument( fileURL );
    if ( document )
    {
		var documentDOM = fl.getDocumentDOM();
		if ( documentDOM )
		{
			var timeline = documentDOM.getTimeline();
			if ( timeline )
			{
				filterLayers( timeline, layerName );
			}
		}
 
		document.publish();
		document.save();
		document.close();
    }
}
 
function layerToSWF( layerName, fileURL, fileName, parentDirURL )
{
	var newFileURL = parentDirURL + layerName + ".fla";
 
	// create new file as copy
	var success = FLfile.copy( fileURL, newFileURL );
	if ( success )
	{
		// publish new file
		publishNewFile( newFileURL, layerName );
		// remove new file (but leave SWF).
		FLfile.remove( newFileURL );
	}
	else
	{
		alert( "Copy from\n" + fileURL +"\nto\n" + newFileURL + "\nfailed." );
	}
 
	return success;
}
 
function timelineToSWFs( timeline, fileURL, fileName, parentDirURL )
{
	var layers = timeline.layers,
		layer,
		i = 0;
		c = layers.length;
 
	for ( i; i < c; i++ )
	{
		layer = layers[ i ];
 
		if ( layer && layer.layerType === "normal" && layer.name )
		{
			if ( ! layerToSWF( layer.name, fileURL, fileName, parentDirURL ) )
			{
				break;
			}
		}
	}
}
 
function layersToSWFs()
{
	var fileURL = fl.browseForFileURL( "open" );
	if ( fileURL )
	{
		var document = fl.openDocument( fileURL );
		if ( document )
		{
			var documentDOM = fl.getDocumentDOM();
			if ( documentDOM )
			{
				var timeline = documentDOM.getTimeline();
				if ( timeline )
				{
					var parentDirURL = fileURL.split( "/" );
 
					var fileName = parentDirURL.pop();
						fileName = fileName.split( "." );
						fileName.pop();
						fileName = fileName.join( "." );
 
					parentDirURL = parentDirURL.join( "/" ) + "/" + fileName;
 
					timelineToSWFs( timeline, fileURL, fileName, parentDirURL );
				}
			}
		}
	}
}
 
// http://blog.derRaab.com - saves you some time - get yourself some fresh air! ;)
layersToSWFs();

Have a nice day!

3 thoughts on “Publish Flash layers to single SWFs”

  1. I was wondering is it possible to do this but output a image sequence instead of swfs?

  2. I know this is years old, but it is exactly what I want to do…
    Tried it out (with CS6) and it sort of works – but….
    it seems that it includes the first layer in all of the swf output files
    So I have layers a,b,c,d in file test
    testa.swf shows layer a
    testb.swf shows layer a and b
    testc.swf shows layer a and c
    testd.swf shows layer a and d

    It’s easy enough to fix by inserting a blank layer, but I thought I’d let you know!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.