Skip to main content
Participating Frequently
February 22, 2024
Answered

jsfl for exporting symbols from library to PNG

  • February 22, 2024
  • 1 reply
  • 1580 views

Hello,

 

I've been searching in this community for a while to to what I described in the title, I have a library full of icons which i gave a proper name. It would be great if I can export a batch with one click of a button. 

I found the following script (at the bottom of this post) which I turned into a command.

 

Found in this topic: https://community.adobe.com/t5/animate-discussions/exporting-graphics-from-the-library/m-p/10870471

I select my library items --> then click the command --> select a specific folder --> save.

 

Then I get the message 0 image(s) exported.

 

 

Is this because the jsfl  was made for an older version? Or is there something wrong with the code? 

Thanks for reading!

 

Erik

 

-----

 

function exportLibraryImagesToFiles() {
var doc = fl.getDocumentDOM();
if (!doc) return;
 
var selectedItems = doc.library.getSelectedItems();
if (!selectedItems.length) return;
 
var folder = fl.browseForFolderURL("Choose an output directory.");
if (!folder) return;
 
var i, t, sym, bmpName;
var count = 0;
 
for (i = 0; i < selectedItems.length; i++) {
sym = selectedItems[i];
if (sym.itemType != "bitmap") {
continue;
}
bmpName = sym.name.split("/").pop();
// strip original extension
t = bmpName.lastIndexOf(".");
if (t != -1 && ["jpg", "jpeg", "png", "gif", "bmp", "psd"].indexOf(bmpName.substr(t + 1).toLowerCase()) != -1) {
bmpName = bmpName.substr(0, t);
}
// do the thing
sym.exportToFile(folder + "/" + bmpName + "." + (sym.compressionType == "photo" ? "jpg" : "png"));
count++;
}
alert(count + " image(s) exported.");
}
 
exportLibraryImagesToFiles();
    Correct answer Vladin M. Mitov

    Here is the revised code. Since there is no method for exporting selected library items, we have to temporarily place them on the stage, export them, and then delete them.

    function exportLibraryImagesToFiles() {
    	
    	if( parseInt( fl.version.split(" ")[1].split(",")[0] ) < 12 ){
    		alert( "This command works with Flash CS6 and above." )
    		return;
    	}
    	
    	var doc = fl.getDocumentDOM();
    	
    	if ( !doc ) return;
    
    	var selectedItems = doc.library.getSelectedItems();
    	if ( !selectedItems.length ) return;
    
    	var folder = fl.browseForFolderURL( "Choose an output directory." );
    	if ( !folder ) return;
    
    	var i, t, sym, bmpName, xPath, count = 0;
    
    	for( i = 0; i < selectedItems.length; i++ ){
    		
    		sym = selectedItems[ i ];
    		
    		if ( sym.itemType === "bitmap" ) {
    			
    			bmpName = sym.name.split("/").pop();
    			// strip original extension
    			t = bmpName.lastIndexOf(".");
    			if (t != -1 && ["jpg", "jpeg", "png", "gif", "bmp", "psd"].indexOf(bmpName.substr(t + 1).toLowerCase()) != -1) {
    				bmpName = bmpName.substr(0, t);
    			}
    			// do the thing
    			sym.exportToFile( folder + "/" + bmpName + "." + ( sym.compressionType == "photo" ? "jpg" : "png" ) );
    			
    			count++;
    			
    		}else{
    			
    			if( sym.itemType === "graphic" || sym.itemType === "movie clip" ){
    			
    				doc.library.addItemToDocument( {x:0, y:0}, sym.name );
    				xPath = folder + "/" + sym.name.split( "/" ).pop() + ".png";
    				doc.exportInstanceToPNGSequence( xPath, 0, sym.timeline.frameCount );
    				doc.deleteSelection();
    				
    				count += sym.timeline.frameCount;
    			}
    		}
    		
    	}
    	
    	alert( count + " image(s) exported." );
    }
    
    exportLibraryImagesToFiles();



    1 reply

    Vladin M. Mitov
    Inspiring
    February 22, 2024

    From what I can see, this code only exports library items of type "bitmap". Perhaps your icons are symbols?

     

     

    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
    Participating Frequently
    March 4, 2024

    Hi Vladin,

     

    Thank you for the reply!

    It's true, the library is filled with symbols (graphics consisting of 1 frame). I'm not trained in reading the code, so I appreciate you taking a look. Meanwhile, I've manually exported the items (right-click -> export PNG-sequence). This results in a PNG with the correct name (the same name as the one given to the graphic in the library). That's actually what I want, but for 100 symbols at once. Could this still be achieved by tweaking the jsfl file a bit?

    kglad
    Community Expert
    Community Expert
    March 4, 2024

    there's no jsfl for that.