Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Batch convert .fla to .png, rescaling as we go
// Based on Dan Catt's https://github.com/revdancatt/CAT422-glitch-location-viewer/blob/master/tools/convert-fla-to-png.jsfl
// Updated by David Wilkinson (@dopiaza):
// - scale fla before exporting as png
// - close file if exception thrown
// Open a folder of fla files, and then output them in a parallel
// folder called "output"
//
// This is very rough and ready and needs babysitting for the files
// that don't work.
//
// Good luck!
var scale = 2.0
var folder = fl.browseForFolderURL("Choose a folder to publish:");
var files = FLfile.listFolder(folder + "/*.fla", "files");
for (file in files) {
var curFile = files[file];
// Keep track of which document is open so we can close it if exception
var openDom = null;
// open document, export, and close
try {
fl.openDocument(folder + "/" + curFile);
var exportFileName = folder + '/../png/' + fl.getDocumentDOM().name.replace('.fla','.png');
var originalDom = fl.getDocumentDOM();
openDom = originalDom;
originalDom.selectAll();
originalDom.clipCopy();
var w = originalDom.width;
var h = originalDom.height;
fl.closeDocument(originalDom);
openDom = null;
fl.createDocument();
dom = fl.getDocumentDOM();
openDom = dom;
dom.width = w * scale;
dom.height = h * scale;
dom.clipPaste();
dom.selectAll();
dom.group();
dom.scaleSelection(scale, scale);
dom.align("vertical center", true);
dom.align("horizontal center", true);
dom.exportPNG(exportFileName, true, true);
dom.close(false);
openDom = null;
} catch(er) {
fl.trace(curFile);
fl.trace(er);
fl.trace('----------------');
if (openDom != null) {
openDom.close(false);
}
}
}
@tmorelli

This comment has been minimized.

Copy link

@tmorelli tmorelli commented Jul 17, 2014

Thanks! It will die when it finds an fla with a locked layer. I added this check. After you do the select all, make sure you have something:
originalDom.selectAll();
var theSelectionArray = originalDom.selection;
if (theSelectionArray.length > 0){

...}

else{
fl.closeDocument(originalDom);
openDom= null;
}

I couldnt get it to automatically unlock the layers, but if you are doing tons of flas at once and dont care if you miss some, this little tweak will keep it from crashing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment