Script to export all Anroid-relevant resolutions for each layer in an artboard

Context

For Android development I am trying to create a script for Illustrator that exports my vector-work from each layer to the .png-format, whereby each export has to be provided in all density (ppi)-formats Android needs. I know that I can export images like:

png24_settings = function ( ) {
    var options = new ExportOptionsPNG24();
    options.antiAliasing = true;
    options... = ...
    ...
    return options;
}

var file_destination = "Some string with the destination path and name"
var doc = app.activeDocument; //Retrieve active document
doc.exportFile(file_destination, ExportType.PNG24, png24_settings() ); // Exports visible canvas

Each layer can be exported by looping over the layers and setting all other layers invisible. However, I cannot create a loop with the given function that helps me with my density problem.

Question

The exportFile()-func does not take any resolution parameter and, thus, cannot be used to create the different density-files needed for Android (hdpi, xhdpi, ...). Which option do I have to export each single layer in all the necessary ppi-formats?

1 answer

  • answered 2020-11-28 13:20 Markus

    With some consultation of the docs I was able to solve my problem. There exists a function called imageCapture() which contains a resolution-parameter for the desired ppi (link to docs). Note that this function will export all visible layers on an artboard. Thus, the iteration for the export needs to take care of visibility.

    With this information, I can iterate over each layer in my artboard and do the export. The script would look like the one provided below. I know its a little exhaustive, but I hope it helps to get a clear understanding.

    Provide setup information

    // Provide density-information necessary for android
    android_densities = [
          120 // ldpi
        , 160 // mdpi
        , 240 // hdpi
        , 320 // xhdpi
        , 480 // xxhdpi
        , 640 // xxxhdpi
    ]
    
    // Provide settings for image export which takes the respective density as argument
    setOptions = function(density) {
        var options = new ImageCaptureOptions();
       
        options.artBoardClipping = true;
        options.resolution       = density // Your desired ppi
        options...                         // Provide other options you'd also like to implement.
        
        return options;
    }
    
    
    // Initialize current document as document to work with
    var doc = app.activeDocument; //Gets the active document
    
    // Retrieve current directory of illustrator-document as default path to save to
    // If you want to save anywhere else, just provide it here
    var filePath = (app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/');
    
    // Select current artboard according to current document
    // This is the artboard you want to export your layers from
    var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];
    

    Note: Android-densities are provided here in the docs.

    Iterative export

    // Export, for example, all visible layers.
    // Thus, all layers are first iterated, checked for visibility and set invisible.
    // Because of the last step all elements have to be cached in an array (= export_layers)
    export_layers= []; // Store layers to export
    for(var i=0; i<doc.layers.length; i++){
        var layer = doc.layers[i];
        if(layer.visible){
            export_layers.push(i);
            doc.layers[i].visible=false;
        };
    }
    
    // Do exports
    for(var exportElement=0; exportElement<export_layers.length; exportElement++) {
        
        // Get layer to export
        var layer = doc.layers[export_layers[exportElement]];
        
        // Set this layer visible (it is the only layer visible now, because imageCapture will export all visible layers)
        doc.layers[export_layers[exportElement]].visible=true;
        
        // Iterate over all densities
        for(var densityElement=0; densityElement<android_densities.length; densityElement++) {
            
            // Provide path and name for output image
            var destination_of_file = new File(filePath + "/" + layer.name + "_" + String(android_densities[densityElement]) + ".png");
            
            // Save file with resolution
            doc.imageCapture(destination_of_file, activeAB.artboardRect, setOptions(android_densities[densityElement]));
        }
    
        // Set visibility to false again for next layer to be exported correctly
        doc.layers[export_layers[exportElement]].visible=false;
    }
    
    
    // Finally, reset the visibility-status prior to the export
    for(var resetElement=0; i<export_layers.length; resetElement++) {
        doc.layers[export_layers[resetElement]].visible=true;
    }
    

    After that you should have all layers in all densities in your specified folder. Of course this can be much more elegant by providing a folder for each resolution and so on - I just tried to keep it to the point for the answer :)