actionscript-3flashjsfl

JSFL Replace image for library image


Im trying to make a script using JSFL but I have the following problem...

To understand what Im trying to do, first I used a script to export all images in the library, because I need to make a process to all PNG files.

Now I want to reinsert the images on the library.

If I create a new item I lose all references and that's not useful and I have duplicated items.

What I need to do is re-import the image for each item. I mean, the same I do when a right-click on the library item->properties->Import...

I was trying to use this script but it doesn't work. Anyone can help me?

var folderURI = fl.browseForFolderURL('Select folder where all images should be exported as *.PNG');
var doc = fl.getDocumentDOM();

if(doc)
{
    fl.outputPanel.trace("Start");
    var library = doc.library;
    var allLibItems = library.items;
    var item;
    var itemName;

    for (var i = 0; i<allLibItems.length; ++i) 
    {
        item = allLibItems[i];//only images will be processed
        if(item.itemType == "bitmap")
        {
            itemName = item.name.slice(item.name.lastIndexOf("/")+1,item.name.lastIndexOf("."));
            //Find proccesed image on the directory selected before
            //and replace the sourceFilePath (I think this is what I need to use a new image but is not working)
            item.sourceFilePath = folderURI + "/" + itemName +".png"

            //This returns 'false'
            fl.outputPanel.trace(library.updateItem(item.name));
            //item.sourceFilePath has the old value, I don't understand why it was not changed
            fl.outputPanel.trace(folderURI + "/" + itemName +".png" + " = " + item.sourceFilePath);
        }

    }
}

Solution

  • Finally, I unzipped the FLA file, it's like a zip and I have all images inside, so it's easy to replace them.

    The problem that I had first was that images didnt change because in Flash you have an option to compress images when you create the swf, that's why I didnt see any change on the final result. (right click on the image inside the library -> Properties -> Compression)

    I used JSFL to iterate all images (like the example above) and set Compression: "Lossless" instead of "Photo (JPG)".

    Obviously this is only a good solution for me because Im using an external tool to compress images with a really great resolution and low size.

    You can use JSFL to iterate all images and set Compression: "Photo (JPG)" to all images, with the quality that you want, but probably the result will be different.

    Regards