Illustrator script: individual layer multiformat export.

New Here ,
Feb 23, 2021

Copy link to clipboard

Copied

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

* @author 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);

}

}

}

TOPICS
Import and export, Scripting

Views

98

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Feb 23, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Feb 23, 2021

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Feb 24, 2021

Copy link to clipboard

Copied

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.");

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Feb 25, 2021

Copy link to clipboard

Copied

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:

Andres5EAE_1-1614260232600.png

 

 

and this is what the exported .ai looks like

Andres5EAE_0-1614260140814.png

 

 

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

 

again, thank you very much for the help.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Feb 25, 2021

Copy link to clipboard

Copied

can you share the test file? on google drive or dropbox or something like that?

also upload those output files. in the layers panel preview, it looks like there's some art there.. perhaps it's just not on the artboard? 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Feb 25, 2021

Copy link to clipboard

Copied

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.

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Feb 26, 2021

Copy link to clipboard

Copied

I've not followed this thread, but on the chance that this will help you, here is how to resize an artboard around its centre:

https://community.adobe.com/t5/illustrator/resize-all-artboards-on-all-open-documents/m-p/11645072#M... 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Feb 26, 2021

Copy link to clipboard

Copied

Awesome! thank you very much. This really helped a lot! I was able to add to this script so it also centers the image. My next step will be to try to combine with the .ai exporting script we had earlier 😄

 

if ( app.documents.length > 0) {
doc = app.activeDocument;

// Get the active Artboard index
var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];

// Get the right most X point of the Artboard
// artboardX + artboardWidth
var artboardRight = activeAB.artboardRect[0] + activeAB.artboardRect[2];
// Get the bottom most Y point of the Artboard
// artboardY + artboardHeight
var artboardBottom = activeAB.artboardRect[1] + activeAB.artboardRect[3];

// The page item you want to move. Reference it how you will. This just
// obviously grabs the first pageItem in the document.
var myPageItem = doc.pageItems[0];

// Here is where the magic happens. Set the position of the item.
// [0,0] would be at the top left, so we have to compensate for the artboard
// height and width. We add item's height then split it for the vertical
// offset, or we'd end up BELOW the artboard.
// Then we subtract the item's width and split the difference to center the
// item horizontally.
var horziontalCenterPosition = (artboardRight - myPageItem.width)/2;
var verticalCenterPosition = (artboardBottom + myPageItem.height)/2;

myPageItem.position = [horziontalCenterPosition, verticalCenterPosition];
}
var w = prompt("width (in inches)") * 72;
var h = prompt("height (in inche)") * 72;
var docs = app.documents;
for (var i = 0; i < docs.length; i++) {
docs[i].activate();
for (var j = 0; j < activeDocument.artboards.length; j++) {
var AB = activeDocument.artboards[j];
var xCentre = AB.artboardRect[0] + ((AB.artboardRect[2]-AB.artboardRect[0])/2);
var yCentre = AB.artboardRect[1] + ((AB.artboardRect[3]-AB.artboardRect[1])/2);
AB.artboardRect = [xCentre - w/2, yCentre + h/2, xCentre + w/2, yCentre - h/2];
}
}

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Feb 26, 2021

Copy link to clipboard

Copied

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.");

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Andres5EAE AUTHOR LATEST
New Here ,
Feb 26, 2021

Copy link to clipboard

Copied

Oh, man! you're the best!. This is definitely perfect. it works great and I really dig the way you explain the lines you added so I understand what they do and can get better at this scripting thing myself, really grateful for that.

I just have a couple of details I'd like to get right and it would definitely save me tons of time working on these files.

 

1. the first one is that I can't get the select folder to save function right. For example, if the path folder I select is: computer/test/save folder the folders will get saved at computer/test, so one folder on top of the one selected. Any idea why this happens? or how to fix it?

 

2. I was trying to work on an option to select a new size for the exported file. like a resizing to a specific size since I want all of these to be 8x8 inches. I am not sure how to integrate it to the script you just gave me (I tried a couple of things but failed miserably) but this is the script I found and I've been working on. (it works great on its own and I like that it resizes in relation to the center of the artboard)

 

var w = prompt("width (in inches)") * 72;
var h = prompt("height (in inche)") * 72;
var docs = app.documents;
for (var i = 0; i < docs.length; i++) {
docs[i].activate();
for (var j = 0; j < activeDocument.artboards.length; j++) {
var AB = activeDocument.artboards[j];
var xCentre = AB.artboardRect[0] + ((AB.artboardRect[2]-AB.artboardRect[0])/2);
var yCentre = AB.artboardRect[1] + ((AB.artboardRect[3]-AB.artboardRect[1])/2);
AB.artboardRect = [xCentre - w/2, yCentre + h/2, xCentre + w/2, yCentre - h/2];
}
}

 

 

Thanks again! and again!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more