Skip to main content
February 23, 2021
Answered

Illustrator script: individual layer multiformat export.

  • February 23, 2021
  • 2 replies
  • 3022 views

Hi Everyone. I've been trying to figure out how to put together a script that would allow me to export individual layers into different formats: .ai, .pdf, .png, .dxf, .eps, .jpg, .svg. I really have very low experience and understanding of scripts, so I'd really appreciate it if someone could point me in the right direction.

 

Basically, I have a file with different illustrations on each layer, I want to first save each layer as a new AI file and hopefully from that file (or maybe at the same time) export to all the mentioned formats.

 

I tried with other multiexporter scripts but didn't find anyone that would do the .ai save and then the exports. And also had trouble with most where instead of exporting the individual layers (individual illustrations) it exports the whole artboard with all the illustrations in it, which is not what I need.

 

Does anyone know of a script that might be able to do this? if it possible to do it with just one script?

 

I'd really appreciate any help I can get.

 

 

P.S.: I've been working with a script from Carlos Canto (below), trying to add the .ai save option unsuccessfully. But wasn't able to get it to work. It doesn't export the individual layers, rather the whole artboard with all the images. So really wondering if I'm doing something wrong before running the script?

 

#target Illustrator

// script.name = exportLayersAsCSS_PNGs.jsx;

// script.description = mimics the Save for Web, export images as CSS Layers (images only);

// script.requirements = an open document; tested with CS5 on Windows.

// script.parent = carlos canto // 05/24/13; All rights reseved

// script.elegant = false;

/**

* export layers as PNG

* @7111211 Niels Bosma

*/

// Adapted to export images as CSS Layers by CarlosCanto

if (app.documents.length>0) {

main();

}

else alert('Cancelled by user');

function main() {

var document = app.activeDocument;

var afile = document.fullName;

var filename = afile.name.split('.')[0];

var folder = afile.parent.selectDlg("Export as CSS Layers (images only)...");

if(folder != null)

{

var activeABidx = document.artboards.getActiveArtboardIndex();

var activeAB = document.artboards[activeABidx]; // get active AB

var abBounds = activeAB.artboardRect;// left, top, right, bottom

showAllLayers();

var docBounds = document.visibleBounds;

activeAB.artboardRect = docBounds;

var options = new ExportOptionsPNG24();

options.antiAliasing = true;

options.transparency = true;

options.artBoardClipping = true;

var n = document.layers.length;

hideAllLayers ();

for(var i=0; i<n; ++i)

{

//hideAllLayers();

var layer = document.layers;

layer.visible = true;

var file = new File(folder.fsName + '/' +filename+ '-' + i+".png");

document.exportFile(file,ExportType.PNG24,options);

layer.visible = false;

}

showAllLayers();

activeAB.artboardRect = abBounds;

}

function hideAllLayers()

{

forEach(document.layers, function(layer) {

layer.visible = false;

});

}

function showAllLayers()

{

forEach(document.layers, function(layer) {

layer.visible = true;

});

}

function forEach(collection, fn)

{

var n = collection.length;

for(var i=0; i<n; ++i)

{

fn(collection);

}

}

}

This topic has been closed for replies.
Correct answer Disposition_Dev

Yep, You0re absolutely right. It did have the art out of the canvas. So this has been extremely helpful for the first part of what I need. I really appreciate it. Now I'm trying to figure out a way to have the exported ai. resize the artboard and center the art before saving it. this is what I tried to do but it didn't go well 😞 

 

#target Illustrator
function exportLayersAsNewAiFiles()
{
var docRef = app.activeDocument;
var docPath = docRef.fullName;
var layers = docRef.layers;

 

//change this to whatever you want
var savePath = "~/Desktop/";

//or if you'd prefer a dialog to select a folder
//use this instead
// var saveFolder = Folder.selectDialog("Select a folder to export your files.");
// if(!saveFolder)
// {
// alert("User cancelled folder select dialog. Exiting.");
// return;
// }
// var savePath = saveFolder.fullName;

 


//make sure there actually is a folder to save into
if(!Folder(savePath).exists)
{
Folder(savePath).create();
}

//process each layer.
loopLayers(layers,saveLayerAsAi);

 

 

 

 

function loopLayers(layers,func)
{
for(var x=0;x<layers.length;x++)
{
func(layers[x]);
}
}

function makeCopyGroup(layer)
{
var copyGroup = layer.groupItems.add();
for(var x = layer.pageItems.length-1;x>=1;x--)
{
layer.pageItems[x].duplicate(copyGroup);
}
return copyGroup;
}

function ungroup(group)
{
for(var x=group.pageItems.length-1;x>=0;x--)
{
group.pageItems[x].moveToBeginning(group.parent);
}

}

 

function saveLayerAsAi(layer)
{
layer.visible = true;
layer.locked = false;

var copyGroup = makeCopyGroup(layer);

var newDoc = app.documents.add();
copyGroup.moveToBeginning(newDoc);
newDoc.layers[0].name = layer.name;

ungroup(copyGroup);

var aDoc = app.activeDocument;

app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;

var abIdx = aDoc.artboards.getActiveArtboardIndex();

var actAbBds = aDoc.artboards[abIdx].artboardRect;

var obj2move = aDoc.selection[0];

obj2move.position = new Array ((actAbBds[2]-actAbBds[0])/2 - obj2move.width/2, (actAbBds[3]-actAbBds[1])/2 + obj2move.height/2);

newDoc.saveAs(File(savePath + layer.name + ".ai"));
newDoc.close();
docRef.activate();

}
}

if(app.documents.length)
exportLayersAsNewAiFiles();
else
alert("Please open a document first.");

 

 

 

 

 

This scripting thing is turning out to be way trickier than I expected. Any ideas on how I can move forward with this one? 

 

Again, thank you very much for guiding me here.

 


Here you go. I added 2 new functions.

 

  • one called "centerArtworkOnArtboard" that takes 2 arguments:
    • You'll want to use this one if you need to maintain consistent artboard sizes throughout all exports.
    • arguments 
      • art: any art item in an illustrator document. pageItem, groupItem, textItem etc.
      • artboard: the artboard object you want to align the artwork to. there will probably only be one of these, but just in case you get fancier down the line, you can still reuse this function if you had a file with multiple artboards

 

  • And another function called "fitArtboardToArt" that takes one argument:
    • This one will leave the art where it is, and instead resize/reposition the artboard so that it fits the artwork. 
    • arguments
      • art: any art item in an illustrator document. pageItem, groupItem, textItem etc.
#target Illustrator

function exportLayersAsNewAiFiles()
{
    var docRef = app.activeDocument;
    var docPath = docRef.fullName;
    var layers = docRef.layers;



    //change this to whatever you want
    var savePath = "~/Desktop/";

    //or if you'd prefer a dialog to select a frolder
    //use this instead
    // var saveFolder = Folder.selectDialog("Select a folder to export your files.");
    // if(!saveFolder)
    // {
    // alert("User cancelled folder select dialog. Exiting.");
    // return;
    // }
    // var savePath = saveFolder.fullName;



    //make sure there actually is a folder to save into
    if (!Folder(savePath).exists)
    {
        Folder(savePath).create();
    }

    //process each layer.
    loopLayers(layers, saveLayerAsAi);



    function loopLayers(layers, func)
    {
        for (var x = 0; x < layers.length; x++)
        {
            func(layers[x]);
        }
    }

    function makeCopyGroup(layer)
    {
        var copyGroup = layer.groupItems.add();
        for (var x = layer.pageItems.length - 1; x >= 1; x--)
        {
            layer.pageItems[x].duplicate(copyGroup);
        }
        return copyGroup;
    }

    function ungroup(group)
    {
        for (var x = group.pageItems.length - 1; x >= 0; x--)
        {
            group.pageItems[x].moveToBeginning(group.parent);
        }

    }


    //assuming you want to maintain the same artboard dimensions for everything
    function centerArtworkOnArtboard(art,artboard)
    {
        var abRect = artboard.artboardRect;
        var abCenter = [(abRect[2]-abRect[0])/2,(abRect[3]+abRect[1])/2];

        art.left = abCenter[0] - art.width/2;
        art.top = abCenter[1] + art.height/2;
    }

    //or use this instead if you want to fit the artboard to the artwork instead
    function fitArtboardToArt(art)
    {
        app.selection = null;
        art.selected = true;
        app.activeDocument.fitArtboardToSelectedArt(0);
    }



    function saveLayerAsAi(layer)
    {
        layer.visible = true;
        layer.locked = false;

        var copyGroup = makeCopyGroup(layer);

        var newDoc = app.documents.add();
        copyGroup.moveToBeginning(newDoc);
        newDoc.layers[0].name = layer.name;

        
        //use this to center the artwork on the existing artboard
        centerArtworkOnArtboard(copyGroup,newDoc.artboards[0]);

        //or use this one instead if you want to resize the artboard
        //to fit the artwork instead
        // fitArtboardToArt(copyGroup);
        

        ungroup(copyGroup);

        // obj2move.position = new Array((actAbBds[2] - actAbBds[0]) / 2 - obj2move.width / 2, (actAbBds[3] - actAbBds[1]) / 2 + obj2move.height / 2);

        newDoc.saveAs(File(savePath + layer.name + ".ai"));
        newDoc.close();
        docRef.activate();

    }
}

if (app.documents.length)
    exportLayersAsNewAiFiles();
else
    alert("Please open a document first.");

 

2 replies

Disposition_Dev
Legend
February 24, 2021

So.. I THINK i understand what you're trying to do. At least the first step of it; saving each layer as its own .ai file. 

 

Please try this out. Unfortunately it's not as clean as I had hoped it would be, but it still does the trick with my simple test documents. Test it out.. Fork the code at https://github.com/wdjsdev/public_illustrator_scripts.git

#target Illustrator
function exportLayersAsNewAiFiles()
{
    var docRef = app.activeDocument;
    var docPath = docRef.fullName;
    var layers = docRef.layers;



    //change this to whatever you want
    var savePath = "~/Desktop/";

    //or if you'd prefer a dialog to select a folder
    //use this instead
    // var saveFolder = Folder.selectDialog("Select a folder to export your files.");
    // if(!saveFolder)
    // {
    //  alert("User cancelled folder select dialog. Exiting.");
    //  return;
    // }
    // var savePath = saveFolder.fullName;




    //make sure there actually is a folder to save into
    if(!Folder(savePath).exists)
    {
        Folder(savePath).create();
    }

    //process each layer.
    loopLayers(layers,saveLayerAsAi);


    







    function loopLayers(layers,func)
    {
        for(var x=0;x<layers.length;x++)
        {
            func(layers[x]);
        }   
    }

    function makeCopyGroup(layer)
    {
        var copyGroup = layer.groupItems.add();
        for(var x = layer.pageItems.length-1;x>=1;x--)
        {
            layer.pageItems[x].duplicate(copyGroup);
        }
        return copyGroup;
    }

    function ungroup(group)
    {
        for(var x=group.pageItems.length-1;x>=0;x--)
        {
            group.pageItems[x].moveToBeginning(group.parent);
        }

    }



    function saveLayerAsAi(layer)
    {
        layer.visible = true;
        layer.locked = false;

        var copyGroup = makeCopyGroup(layer);

        var newDoc = app.documents.add();
        copyGroup.moveToBeginning(newDoc);
        newDoc.layers[0].name = layer.name;

        ungroup(copyGroup);
        
        newDoc.saveAs(File(savePath + layer.name + ".ai"));
        newDoc.close();
        docRef.activate();

    }
}

if(app.documents.length)
    exportLayersAsNewAiFiles();
else
    alert("Please open a document first.");
February 25, 2021

Hey! thank you very much for this. It does look like the first step, the one that has been giving me more trouble. But sadly it doesn't seem to work properly for me. I mean I run the script and it does look like it is doing the right thing. But the files I get look blank, like a blank artboard. Although it gets tee right name and even the little thumbnail beside the name on the layer panel shows the image that should be on the artboard it still appears blank. Like this:

 

 

This is the file I'm testing with:

 

 

and this is what the exported .ai looks like

 

 

Maybe I'm doing something wrong before running the script?

 

again, thank you very much for the help.

Disposition_Dev
Disposition_DevCorrect answer
Legend
February 26, 2021

Yep, You0re absolutely right. It did have the art out of the canvas. So this has been extremely helpful for the first part of what I need. I really appreciate it. Now I'm trying to figure out a way to have the exported ai. resize the artboard and center the art before saving it. this is what I tried to do but it didn't go well 😞 

 

#target Illustrator
function exportLayersAsNewAiFiles()
{
var docRef = app.activeDocument;
var docPath = docRef.fullName;
var layers = docRef.layers;

 

//change this to whatever you want
var savePath = "~/Desktop/";

//or if you'd prefer a dialog to select a folder
//use this instead
// var saveFolder = Folder.selectDialog("Select a folder to export your files.");
// if(!saveFolder)
// {
// alert("User cancelled folder select dialog. Exiting.");
// return;
// }
// var savePath = saveFolder.fullName;

 


//make sure there actually is a folder to save into
if(!Folder(savePath).exists)
{
Folder(savePath).create();
}

//process each layer.
loopLayers(layers,saveLayerAsAi);

 

 

 

 

function loopLayers(layers,func)
{
for(var x=0;x<layers.length;x++)
{
func(layers[x]);
}
}

function makeCopyGroup(layer)
{
var copyGroup = layer.groupItems.add();
for(var x = layer.pageItems.length-1;x>=1;x--)
{
layer.pageItems[x].duplicate(copyGroup);
}
return copyGroup;
}

function ungroup(group)
{
for(var x=group.pageItems.length-1;x>=0;x--)
{
group.pageItems[x].moveToBeginning(group.parent);
}

}

 

function saveLayerAsAi(layer)
{
layer.visible = true;
layer.locked = false;

var copyGroup = makeCopyGroup(layer);

var newDoc = app.documents.add();
copyGroup.moveToBeginning(newDoc);
newDoc.layers[0].name = layer.name;

ungroup(copyGroup);

var aDoc = app.activeDocument;

app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;

var abIdx = aDoc.artboards.getActiveArtboardIndex();

var actAbBds = aDoc.artboards[abIdx].artboardRect;

var obj2move = aDoc.selection[0];

obj2move.position = new Array ((actAbBds[2]-actAbBds[0])/2 - obj2move.width/2, (actAbBds[3]-actAbBds[1])/2 + obj2move.height/2);

newDoc.saveAs(File(savePath + layer.name + ".ai"));
newDoc.close();
docRef.activate();

}
}

if(app.documents.length)
exportLayersAsNewAiFiles();
else
alert("Please open a document first.");

 

 

 

 

 

This scripting thing is turning out to be way trickier than I expected. Any ideas on how I can move forward with this one? 

 

Again, thank you very much for guiding me here.

 


Here you go. I added 2 new functions.

 

  • one called "centerArtworkOnArtboard" that takes 2 arguments:
    • You'll want to use this one if you need to maintain consistent artboard sizes throughout all exports.
    • arguments 
      • art: any art item in an illustrator document. pageItem, groupItem, textItem etc.
      • artboard: the artboard object you want to align the artwork to. there will probably only be one of these, but just in case you get fancier down the line, you can still reuse this function if you had a file with multiple artboards

 

  • And another function called "fitArtboardToArt" that takes one argument:
    • This one will leave the art where it is, and instead resize/reposition the artboard so that it fits the artwork. 
    • arguments
      • art: any art item in an illustrator document. pageItem, groupItem, textItem etc.
#target Illustrator

function exportLayersAsNewAiFiles()
{
    var docRef = app.activeDocument;
    var docPath = docRef.fullName;
    var layers = docRef.layers;



    //change this to whatever you want
    var savePath = "~/Desktop/";

    //or if you'd prefer a dialog to select a frolder
    //use this instead
    // var saveFolder = Folder.selectDialog("Select a folder to export your files.");
    // if(!saveFolder)
    // {
    // alert("User cancelled folder select dialog. Exiting.");
    // return;
    // }
    // var savePath = saveFolder.fullName;



    //make sure there actually is a folder to save into
    if (!Folder(savePath).exists)
    {
        Folder(savePath).create();
    }

    //process each layer.
    loopLayers(layers, saveLayerAsAi);



    function loopLayers(layers, func)
    {
        for (var x = 0; x < layers.length; x++)
        {
            func(layers[x]);
        }
    }

    function makeCopyGroup(layer)
    {
        var copyGroup = layer.groupItems.add();
        for (var x = layer.pageItems.length - 1; x >= 1; x--)
        {
            layer.pageItems[x].duplicate(copyGroup);
        }
        return copyGroup;
    }

    function ungroup(group)
    {
        for (var x = group.pageItems.length - 1; x >= 0; x--)
        {
            group.pageItems[x].moveToBeginning(group.parent);
        }

    }


    //assuming you want to maintain the same artboard dimensions for everything
    function centerArtworkOnArtboard(art,artboard)
    {
        var abRect = artboard.artboardRect;
        var abCenter = [(abRect[2]-abRect[0])/2,(abRect[3]+abRect[1])/2];

        art.left = abCenter[0] - art.width/2;
        art.top = abCenter[1] + art.height/2;
    }

    //or use this instead if you want to fit the artboard to the artwork instead
    function fitArtboardToArt(art)
    {
        app.selection = null;
        art.selected = true;
        app.activeDocument.fitArtboardToSelectedArt(0);
    }



    function saveLayerAsAi(layer)
    {
        layer.visible = true;
        layer.locked = false;

        var copyGroup = makeCopyGroup(layer);

        var newDoc = app.documents.add();
        copyGroup.moveToBeginning(newDoc);
        newDoc.layers[0].name = layer.name;

        
        //use this to center the artwork on the existing artboard
        centerArtworkOnArtboard(copyGroup,newDoc.artboards[0]);

        //or use this one instead if you want to resize the artboard
        //to fit the artwork instead
        // fitArtboardToArt(copyGroup);
        

        ungroup(copyGroup);

        // obj2move.position = new Array((actAbBds[2] - actAbBds[0]) / 2 - obj2move.width / 2, (actAbBds[3] - actAbBds[1]) / 2 + obj2move.height / 2);

        newDoc.saveAs(File(savePath + layer.name + ".ai"));
        newDoc.close();
        docRef.activate();

    }
}

if (app.documents.length)
    exportLayersAsNewAiFiles();
else
    alert("Please open a document first.");

 

Charu Rajput
Community Expert
Community Expert
February 23, 2021

Hello,

Did you try MultiExporter script that gives lots of options to export.

Here is the link for the same

https://gist.github.com/TomByrne/7816376 

Best regards
February 23, 2021

Hey! thanks for replying so fast!. I did try that script, but I have a couple of issues there so didn't look further into it. First of all, it doesn't have all the formats I need (.dxf and .ai) and I can only do one format at a time, I would have to re-run the script for each format I want to get, which wouldn't be a big deal but I have some file with 20+ different layers each with different illustrations so I would really want to get it all done in just one run from the script. Although if I was able to add the .dxf and .ai file to this script it might be a very good option. Thank you very much!