Archive for the ‘jsfl’ Category

Recursive PNG to SWF JSFL script - enjoy!

Monday, April 13th, 2009

So if you haven’t noticed yet, a swf with a transparent PNG file compiled on the stage at 0,0 is far smaller than a plain old transparent PNG. I don’t know exactly what Flash is doing to the PNG files, but the file sizes look a lot like JPEG compression sizes, so I suspect it’s compressing the PNGs down to JPEGs and adding the alpha layer…or something like that. This is extremely useful when you need to load many transparent files at runtime.  The problem is that most of the time you get these files in the form of PNGs and I haven’t found a great way to export SWFs from Photoshop - but I’m not Photoshop wizard so there might be a way.

Here’s my way: A nifty JSFL script that recursively searches for PNG files and leaves corresponding SWFs next to them!I’ve noticed it bog down when trying to handle hundreds and hundreds of larger PNGs, so I highly recommend saving all .FLAs you have open before running this since if it can’t handle the load - Flash will crash. I’ll look into a fix for this when I get a moment. Let me know if you think of a good way to prevent it - I haven’t had a spare moment to take a look.

Save this to your commands folder ( download link after the code ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// author: Jeff Yamada
// warning: this might destroy your computer
 
fl.outputPanel.clear();
var dom = fl.getDocumentDOM();
var baseURI = fl.browseForFolderURL("Select a folder.");
var exportedSWFS = new Array();
var folderNumber = 0;
 
findPNGs( baseURI );
 
function findPNGs( folderURI )
{
	var directories = FLfile.listFolder( folderURI, "directories" );
	var files = FLfile.listFolder( folderURI + "/*.png", "files" );
	var i;
	if( directories != false )
	{
		for( i = 0; i < directories.length; i ++ )
		{
			var directory = directories[i];
			var type = FLfile.getAttributes( folderURI + "/" + directory );
			switch( type )
			{
				case "D":
				case "DH":
					findPNGs( folderURI + "/" + directories[i] );
					break;
				default:
			}
		}
	}
	if( files != false )
	{
		for( i = 0; i < files.length; i ++ )
		{
			convertPngToSWF( folderURI, files[i] );
		}
		clearLibrary();
	}
}
 
function convertPngToSWF( pngURI, fileName )
{
	var swfName = fileName.substring( 0, fileName.lastIndexOf( "." ));
	var swfURI = pngURI + "/" + swfName + ".swf";
	exportedSWFS.push( swfURI );
	dom.importFile( pngURI + "/" + fileName );
	dom.exportSWF( swfURI, true );
	clearStage();
}
 
function printReport()
{
	fl.trace( '==== ' + exportedSWFS.length + ' swfs created from pngs ====' );
	fl.trace( exportedSWFS.join( "\n" ));
}
 
function clearStage()
{
	dom.selectAll();
	dom.deleteSelection()
}
 
function clearLibrary()
{
	var libItems = dom.library.items;
	for( var i = 0; i < libItems.length; i ++ )
	{
		var item = libItems[i];
		dom.library.newFolder( "folder"+folderNumber );
		dom.library.moveToFolder( "folder"+folderNumber, item.name );
	}
	folderNumber++;
}
 
printReport();

png2swf.jsfl.zip