Search code examples
javascriptcolorsexportadobe-illustrator

illustrator fillcolor object


Is there a script which can loop through every swatchcolor and each time it makes a copy "layer 1" and fills it with the swatchcolor? So if there is 20 colors in the swatch, then there will be added 20 new layers with different colors.

If yes, could each new layer get the name from the swatch and also be exportet as swatchName.jpg?


Solution

  • Going through the Illustrator JavaScript API you will notice the Document object has a swatches array. All that's left to do is:

    1. loop through each swatch
    2. draw a box of the current swatch colour
    3. export an image

    I recommend using png-24 instead of jpg to avoid compression artefacts.

    Here's a commented script that prompts for an export folder first:

    #target illustrator
    
    //get a reference to the the current document
    var doc = app.activeDocument;
    //...and it's swatches
    var swatches = doc.swatches;
    //select a folder to save images into
    var savePath = Folder.selectDialog( 'Please select a folder to export swatch images into', '~' );
    //exported image dimensions
    var width = 100;
    var height = 100;
    //PNG export options
    var pngExportOpts = new ExportOptionsPNG24();
       pngExportOpts.antiAliasing = false;//keep it pixel perfect 
       pngExportOpts.artBoardClipping = false;//use the path's dimensions (setup above), ignore full document size
       pngExportOpts.saveAsHTML = false;
       pngExportOpts.transparency = true;//some swatches might have transparency
    
    //remove strokes
    doc.defaultStroked = false;
    
    //go through the swatches
    for(var i = 0; i < swatches.length; i++){
       //add a rectangle
       var rect = doc.pathItems.rectangle(0, 0, width, height);  
       //set the fill colour based on the current swatch colour
       rect.fillColor =  swatches[i].color;
    
       //export png
       doc.exportFile( new File( savePath+ '/' + swatches[i].name + '.png'), ExportType.PNG24, pngExportOpts );
       //remove any previous paths (in case of transparent swatches)
       doc.pathItems.removeAll();
    }
    

    It's also worth noting you could parse .ase (Adobe Swatch Exchange) files in a language of choice to export the images, avoiding using Illustrator altogether.