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!

My little Flash library cleanup script

Just save this little code as a .jsfl file:

function editItem( lib, item )
{
	var success = true,
		linkageIdentifier = item.linkageIdentifier,
		itemType = item.itemType,
		path = [];
 
	// separate export symbols from assets
	path.push( ( linkageIdentifier ) ? "_export" : "_asset" );
 
	// don't touch existing folders
	if ( itemType !== "folder" )
	{
		// separate different item types
		path.push( itemType );
 
		success = libItemMoveToFolder( lib, path.join( "/" ), item, false );
	}
 
	return success;
}
 
function libItemMoveToFolder( lib, path, item, replace )
{
	fl.outputPanel.trace( "libItemMoveToFolder( " + lib + ", " + path + ", " + item.name + ", " + replace + " ) CALL" );  
	var folderCreated = lib.newFolder( path ),
		success = false;
 
	if ( folderCreated )
	{
		// Can't move symbol names with empty space
		if ( item.name.indexOf( " " ) )
		{
			// But even this doesn't work - so avoid spaces in item names!
			item.name = item.name.split( " " ).join( "" );
		}
		success = lib.moveToFolder( path, item.name, replace );
		if ( ! success )
		{
			fl.outputPanel.trace( "libItemMoveToFolder( " + lib + ", " + path + ", " + item.name + ", " + replace + " ) Error Can't move item!" );  
		}
	}
	else
	{
		fl.outputPanel.trace( "libItemMoveToFolder( " + lib + ", " + path + ", " + item.name + ", " + replace + " ) Error Can't create folder!" );  
	}
 
	return success;
}
 
function iterateAllItems( lib )
{
	var items = lib.items,
		item,
		c = items.length,
		i = 0,
		success;
 
	for ( i; i < c; i++ )
	{
		item = items[ i ];
 
		if ( item )
		{
			success = editItem( lib, item )
			if ( ! success )
			{
				break;
			}
		}
	}
}
 
// Select .fla
var avatarFLASource = fl.browseForFileURL( "open");
if ( avatarFLASource )
{
	// Open .fla
	var doc = fl.openDocument( avatarFLASource );
	if ( doc )
	{
		var lib = doc.library;
		if ( lib )
		{
			iterateAllItems( lib );
		}
	}
}

If you use empty spaces within your item names these items won’t move at all. I was to busy fixing this issue but maybe you know how to solve this little problem?

Export multiple .fla files using .jsfl

Another very basic information. I had to export multiple .fla files so I wrote my first .jsfl file:

// Binary Directory path (file:// syntax)
var binDir = "file:///Users/.../swf/";
// Source Directory path (file:// syntax)
var srcDir = "file:///Users/.../fla/";
// .fla file names
var fileNames = new Array( "file1", "file2", "file3" );
 
var fileName;
var fileSrc;
var fileBin;
var fla;
 
for ( var i = 0; i < fileNames.length; i++ )
{
	fileName = fileNames[ i ];
	fileSrc = srcDir + fileName + ".fla";
	fileBin = binDir + fileName + ".swf";
 
	fla = fl.openDocument( fileSrc );
	fla.exportSWF( fileBin, true ); 
 
	fl.closeDocument( fla, false );
}