0

I'm using Flash CS6 and I need to save a 32 bit PNG from a vector graphic in 9 different sizes.

16
32
36
48
72
114
128
150
480

How to write a batch export script for that in JSFL?

JSFL Docs (PDF)

Matt
  • 2,981
  • 5
  • 23
  • 33

1 Answers1

3

I have a script that does what you are looking to do. It doesn't appear that you have really attempted to write any code or show any research attempt so if you do end up using this script I would appreciate the credit.

Select a movie clip on the stage then run this command.

Andrew Doll - Multiple Size PNG Exporter

Note: Before running this script export one PNG image using the desired PNG export settings.

fl.getDocumentDOM.exportPNG() accepts 3 paramaters. The first is the string for the file name. The second is a Boolean value that specifies whether to use the current PNG publish settings (true) or to display the Export PNG dialog box (false). The third is a Boolean value that specifies whether to export only the current frame (true) or to export all frames, with each frame as a separate PNG file (false).

Since this script sets the second paramater to true just be sure that the PNG export settings are already set to 32 bit PNG.

// Multiple Size PNG Exporter
// Copyright © 2014 Andrew Doll
// http://www.andrewdollanimation.com/

/* NOTE:Before running this script export one PNG image using the desired PNG export settings.  fl.getDocumentDOM.exportPNG() accepts 3 
** paramaters. The first is the string for the file name.  The second is a Boolean value that specifies whether to use the current PNG 
** publish settings (true) or to display the Export PNG dialog box (false).  The third is a Boolean value that specifies whether to export 
** only the current frame (true) or to export all frames, with each frame as a separate PNG file (false).  Since this script sets the
** second paramater to true just be sure that the PNG export settings are already set to 32 bit PNG.
*/

// Check to see if there is a file open first.
var dom = fl.getDocumentDOM();
if (dom == null)
{
    alert("Please open a file.");
}
else
{
    var sel = [];
    var exportSizeArray = [];
    var folderURI = "";
    var folderLocation = "";
    var pngFileName = "";
    var URI = "";
    var selWidth;
    var selHeight;
    var sideToUse;
    var scaleAmount;

    function setupExportFolder()
    {
        // Create a folder and file name for the PNG files.
        folderLocation = fl.browseForFolderURL("Select a folder.");
        if(folderLocation != null)
        {
            folderURI = folderLocation + "/PNG Exports";
            FLfile.createFolder(folderURI);
            pngFileName = prompt("What would you like to name the png files?");
        }
    }

    // Check if a movie clip on the stage is selected to export PNG images.
    var selectionCheck = dom.selection;
    if(!selectionCheck || !selectionCheck.length)
    {
        alert("Please select a movie clip on the stage.");
    }
    else
    {
        // Set up export sizes in this array.
        exportSizeArray = [16, 32, 64, 128, 256, 512, 1024];

        // Setup export folder
        setupExportFolder();

        if(folderLocation != null && pngFileName != null)
        {
            // Copy the selected artwork from the stage.
            sel = dom.selection[0];
            dom.clipCopy();

            // Calculate the amount to scale the symbol by based on the longest side.
            function calculateScaleAmount(selWidth, selHeight)
            {
                if(selWidth >= selHeight)
                {
                    sideToUse = selWidth;
                }
                else
                {
                    sideToUse = selHeight;
                }
                scaleAmount = exportSizeArray[i]/sideToUse;
                return scaleAmount;
            }

            // Set the width and height of the symbol. Handle this with the size array.
            for (var i = 0; i < exportSizeArray.length; i++)
            {
                // Create a new FLA document.
                fl.createDocument();
                dom = fl.getDocumentDOM();

                // Resize the document to the current export size.
                dom.width = exportSizeArray[i];
                dom.height = exportSizeArray[i];

                // Paste the artwork to the stage.
                dom.clipPaste(true);
                sel = dom.selection[0];
                dom.setAlignToDocument(true);
                selWidth = sel.width;
                selHeight = sel.height;
                calculateScaleAmount(selWidth, selHeight);

                // Scale the artwork to the size of the stage based on the largest side.
                dom.scaleSelection(scaleAmount, scaleAmount, "center");

                // Align to the center of the stage.
                dom.align("vertical center", true);
                dom.align("horizontal center", true);

                // Output the image.
                URI = folderURI + "/" + pngFileName + "_" + exportSizeArray[i] + " x " + exportSizeArray[i] + "_";
                dom.exportPNG(URI, true, true);

                // Close the temporary FLA without saving.
                dom.close(false);
            }
        }
    }
}
andrewdoll
  • 146
  • 5
  • The following JavaScript error(s) occurred: At line 49 of file "/Users/xxxx/Desktop/download/download/Multiple Size PNG Exporter.jsfl": The function clipCut() is currently unavailable. – zszen Jul 19 '13 at 13:30
  • 1
    My apologies for the late reply, but I think I have found out why you were getting that error. First, you have to make sure to place values into the exportSizeArray so it knows what size icons you wish to export. For the example that Matt required the line of code would look like this: // Set up export sizes. exportSizeArray = [16, 32, 36, 48, 72, 114, 128, 150, 480]; And after that is set up, just make sure that you have a symbol on the stage selected. If you follow those two steps you shouldn't get the clipCut error. – andrewdoll Sep 14 '13 at 15:54
  • I just wanted to post an update to this code. Attempting to run this again in Flash CS6 today at work I was running into some interesting issues and had to edit the code a bit. I placed some checks in the code to see if the user has a movie clip selected and if a destination folder was set. Another adjustment I had to make was towards the end of the script I had to place the dom.scaleSelection(scaleAmount, scaleAmount, "center"); before the dom.align. It used to be after and I didn't have a problem, but for some reason now it wasn't placing the scaled item in the center of the stage. – andrewdoll Jan 29 '14 at 06:54