All of us who work with Flash have created our own jsfl commands to adjust the programs to fit our needs, some more simple and other more complex that allow us to automate repetitive tasks. Anyone who knows a little about JavaScript, can create his own commands, being these for Flash or for other Adobe programs which provide API access.
If you want to go further, they can be implemented as program extensions. One of the most interesting projects over jsfl is xJSFL, an open source jsfl framework for the creation of tools and plugins for the Flash program developed by Dave Stewart.
Some of the commands I have created and which I have needed at some time in order to develop repetitive tasks are shown below.
Change image compression type in the library
This command saves you from one of the most tedious task in Flash when you have to work with png files, as it tends to place the imported and pasted images in jpg.
1 2 3 4 5 6 7 8 9 |
//change bitmap compressionType to lossless for (idx in fl.getDocumentDOM().library.items) { if (fl.getDocumentDOM().library.items[idx].itemType == "bitmap") { var myItem = fl.getDocumentDOM().library.items[idx]; myItem.compressionType = "lossless"; myItem.allowSmoothing = true; } } |
Reduce the size of the selected element and place it at a certain point
This is a very simple command but often very helpful, as it repositions elements in the scene.
1 2 3 4 5 6 7 |
//reduce scale 50% and set position to x=0, y=0 var element = fl.getDocumentDOM().selection[0]; element.scaleX = 0.50; element.scaleY = 0.50; element.x = 0; element.y = 0; |
Change text properties
In Flash there is not a styles panel as in other Adobe programs, as Illustrator, Fireworks, etc. Because of that, in order to apply styles to a text, we have to apply each of the properties from the Character and Paragraph panels. To avoid repeating the text formatting, I usually create commands that work as the styles of a Styles panel. One of these commands can be created easily by saving as command the different actions that stay recorded in the History panel. Generally, once the command is saved, it is necessary to make some adjustments so that this works in any situation and not only in which the command has been recorded.
1 2 3 4 5 |
//change text attributes fl.getDocumentDOM().setElementTextAttr('leftMargin', 60); fl.getDocumentDOM().setElementTextAttr('face', 'Verdana'); fl.getDocumentDOM().setFillColor('#0000ff'); |
Modify fla files in a directory
One of the biggest problems with Flash when a big amount of fla files is managed is that, to modify something you have to open the file, modify it and compile it again. If that same modification has to be done in several ‘fla’ files, the solution is to use a command with a recursive function which opens, modify and compiles the different ‘fla’ files. From Flash CS5, instead of using the ‘fla’ format, we can use the XFL format, which allow us to do certain tasks on different ‘fla’ files by simply replacing the changes in XML files, although we do need to recompile the file afterwards.
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 |
//change fla files in folder and subfolders fl.outputPanel.clear(); g_path = "file:///C:/flashesfolder/"; searchInFolder (g_path); function searchInFolder (path) { var filesNames = FLfile.listFolder(path + "*.fla", "files"); for (var k=0; k<filesNames.length; k++) { fl.openDocument (path + filesNames[k]); actions(); } var foldersNames = FLfile.listFolder(path,"directories") fl.trace(foldersNames); for (var h=0; h<foldersNames.length;h++) { var l_path=path+foldersNames[h]+"/"; fl.trace("path:" + l_path); searchInFolder(l_path) } } function actions() { fl.getDocumentDOM().setPlayerVersion("8"); fl.getDocumentDOM().asVersion = 2; fl.getDocumentDOM().publish(); fl.getDocumentDOM().save(); fl.getDocumentDOM().close() } |
Search and replace ActionScript
The following is an experimental command that searchs an ActionScript code and replaces it by another one in all the fla files in a folder. It can not access the code over clips, just as it was used in ActionScript 1 and 2. If anyone wants to try this, I recommend him/her the maximum possible precaution as it could take hours to process a ‘fla’ file and not using it on important files.
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 |
fl.outputPanel.clear(); //folder with fla files var mainPath="file:///C:/flashesfolder/"; //strings to search and replace var findString = "stop();"; var replaceString = "play();"; //execute main function to iterate in folder searchInFolder (mainPath) function searchInFolder (path) { var filesNames = FLfile.listFolder(path + "*.fla","files") for (var k=0;k<filesNames.length;k++) { fl.trace(path + filesNames[k]); fl.openDocument(path + filesNames[k]); var mainTimeline = fl.getDocumentDOM().getTimeline(); iterateLayersAndReplace (mainTimeline); //publish, save and close fl.getDocumentDOM().publish(); fl.getDocumentDOM().saveAndCompact(); fl.getDocumentDOM().close(); } var foldersNames = FLfile.listFolder(path,"directories") for (var h=0; h<foldersNames.length;h++) { var l_path=path+foldersNames[h]+"/"; searchInFolder(l_path) } } function iterateLayersAndReplace (theTimeline) { // loop for layers for ( var i = 0; i < theTimeline.layers.length; i++ ) { var layer = theTimeline.layers[i]; if(layer.layerType == "normal") { // loop for frames for ( var j = 0; j < layer.frames.length; j++ ) { var frame = layer.frames[j]; //replace AS in frames frame.actionScript = frame.actionScript.replace(findString, replaceString); // loop for elements for ( var k = 0; k < frame.elements.length; k++ ) { var element = frame.elements[k]; if(element.elementType == "instance") { if(element.libraryItem.itemType=="movie clip" || element.libraryItem.itemType=="button") { var curr_lib_item = element.libraryItem.name; fl.getDocumentDOM().library.editItem(curr_lib_item); var new_timeline = fl.getDocumentDOM().getTimeline(); iterateLayersAndReplace(new_timeline); } } } } } } } |