Skip to content

Instantly share code, notes, and snippets.

@jpdevries
Created May 3, 2013 22:01
Show Gist options
  • Save jpdevries/5514537 to your computer and use it in GitHub Desktop.
Save jpdevries/5514537 to your computer and use it in GitHub Desktop.
JSFL Script to turn PNG Sequence into Reflected SWF animation
/**
* @language JSFL
*
* @author JP DeVries
* @since 24.08.11
* @version 0.1
*
* @description
* This is a jsfl script that will take all png files from a source folder and create a swf of the sequence playing forwards, then backwards
* The file that is currently open and has focus in the IDE is used as the host for exporting.
* Stage size and publish settings from the host fla are used when exporting.
*
*/
// Set up variables
var doc = fl.getDocumentDOM();
var timeline = doc.getTimeline();
var i;
var saveName;
// Get a source directory
var srcDir = fl.browseForFolderURL("Choose a folder to import PNGs from:");
// Get a save directory.
var saveDir = fl.browseForFolderURL("Choose a folder in which to save your exported SWF:");
if (saveDir && srcDir) {
// Get the Flash document's name, and strip off the final ".fla",
fl.outputPanel.clear();
// Build a base name for the exported files.
saveName = saveDir + "/";
// Grab all png files in the source folder
var fileMask = "*.png";
var fileList = FLfile.listFolder(srcDir + "/" + fileMask, "files");
fileList = reflectFileList(fileList);
timeline.currentFrame = 1;
timeline.clearFrames(0);
// Now process through each one and export it as a swf
for (var i=0; i < fileList.length; i++) {
// Import the file
var URI = srcDir + "/" + fileList[i];
// make room
timeline.currentFrame = i+1;
if(i>0) timeline.insertBlankKeyframe();
// if it's already in the library reuse it, otherwise add it fresh
var _itemExists = doc.library.itemExists(fileList[i]);
if(!_itemExists)doc.importFile(URI);
else doc.library.addItemToDocument({x:document.width/2, y:document.height/2}, fileList[i]);
// Export the file
var fileName = fileList[i].substr(0,fileList[i].length -4);
};
// export the swf
var _fs = saveDir.split('/');
exportSwf(_fs[_fs.length-1]);
// clear the timeline
//doc.getTimeline().clearFrames(0);
}
// creates an auto rewind sequence by reflecting the file list
// ex: 12321 for files 123
function reflectFileList(fileList) {
var _ra = new Array();
for(var i = 0; i < fileList.length; i++) _ra.push(fileList[i]);
for (var i = fileList.length-2; i >= 0; i--) _ra.push(fileList[i]);
return _ra;
}
// save out the swf
function exportSwf(fileName) {
var _name = saveName + fileName + ".swf";
doc.exportSWF(_name, true);
// Log the file in case you need to refer to it
fl.trace("Exported: " + _name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment