Automating Image Creation With JSFL
December 7, 2009 – 9:02 pmA while ago Steve wrote a post about converting images to SWFs to minimise loading times. In the right circumstances it can be a very nice tactic, as it makes such a huge difference.
Anyway, a project manager that Steve was working with heard about this, loved the idea, and by coincidence I am now working with said project manager.
While from my point of view it didn’t really change things from my end, it meant quite a bit of work for the designer.
Because I’m such a nice person, I knocked up some JSFL for him to automate the process, and now I’m sharing it with everyone. Christmas spirit abounds…
The JSFL runs through the FLA’s library, and exports all images, reflecting the library folder structure in the saved path. If you’re new to JSFL then all you have to do is save the JSFL file somewhere, and in Flash go to Commands > Run Command, and select the JSFL.
Feel free to download the JSFL, or just take the code from below:
var doc = fl.getDocumentDOM();
var items = doc.library.items;
var len = items.length;
fl.outputPanel.clear();
var path = fl.browseForFolderURL("Select a folder") + '/';
for (var xi = 0; xi < items.length; xi++){
doc.selectAll();
if (doc.selection.length > 0) doc.deleteSelection();
var item = items[xi];
var currFolder = '';
if (item.itemType == 'bitmap'){
doc.addItem({x: 0, y: 0}, item);
doc.selectAll();
doc.align('top', true);
doc.align('left', true);
var tName = item.name.split('.');
var pArray = tName[0].split('/');
for (var xp = 0; xp < pArray.length - 1; xp++){
currFolder += pArray[xp] + '/';
if (!(FLfile.exists(path + currFolder))) FLfile.createFolder(path + currFolder);
}
doc.exportSWF(path + tName[0] + ".swf");
fl.trace('Exported ' + path + tName[0] + '.swf');
}
}
Incidentally, there are some really useful JSFL scripts from Fuel, on Google Code. I highly recommend having a quick look…

