Skip to main content
azisher
Participating Frequently
February 28, 2017
Answered

Multiple artboards to multiple layers

  • February 28, 2017
  • 2 replies
  • 6905 views

Hello everyone, I have this script to convert multiple layers to multiple artboards, and also copy each layer name to artboards.

The problem is how to reserve the this script process?

I mean to converts artboards to layer and copy artboards name to layer. Thanks

 

#target illustrator 

 

main(); 

 

function main() { 

   

          if ( app.documents.length == 0 ) { return; } 

 

          var doc = app.activeDocument; 

   

          doc.layers[0].hasSelectedArtwork = true; 

   

          doc.fitArtboardToSelectedArt( 0 ); 

   

          doc.selection = null; 

 

          for ( var  i = 1; i < doc.layers.length; i++ ) { 

   

                    doc.artboards.add( [0,0,72,-72] ); 

   

                    doc.layers[i].hasSelectedArtwork = true; 

   

                    doc.fitArtboardToSelectedArt( i ); 

   

                    doc.selection = null; 

 

          }; 

 

};

 

if (app.documents.length == 0) {

    alert("No Open / Active Document Found");

} else {

    var doc = app.activeDocument;

    if (doc.artboards.length == doc.layers.length && doc.layers.length == doc.artboards.length) {

        for (var i = 0, l = doc.artboards.length; i < l; i++) {

            var ab = doc.artboards[i];

            ab.name = doc.layers[i].name;

        }

        alert("Finished:\nRenaming of Artboards to match Layer names is complete");

    } else {

        alert("Opps: This wont work!\n The number of Layers and Artboards do not match");

    }

}

Correct answer Disposition_Dev

here the file:

icon-color.ai - Google Drive


Here you go.

 

function layersFromArtboards()

{

    var docRef = app.activeDocument;

    var layers = docRef.layers;

    var aB = docRef.artboards;

 

    //we will remove this layer after the others have been created.

    var tempLay = layers[0];

    tempLay.name = "Temp";

 

    //loop the artboards

    for(var x=0;x<aB.length;x++)

    {

        var thisAb = aB[x];

        aB.setActiveArtboardIndex(x);

        docRef.selectObjectsOnActiveArtboard();

        var sel = docRef.selection;

 

        //create a new layer

        var newLay = layers.add();

        newLay.name = thisAb.name;

 

        //loop through the selection and move all artwork onto new layer

        //this is only necessary if the artwork on a given artboard is not

        //grouped. This ensures that all the art will be moved, even if there are

        //multiple ungrouped objects.

        for(var a=0;a<sel.length;a++)

        {

            sel[a].moveToEnd(newLay);

        }

    }

 

    tempLay.remove();

}

layersFromArtboards();

2 replies

Participant
September 10, 2021

I got an error using this script. 😞

Error 1302 : No such element line

ab.name = doc.layers.name;

 

Participant
September 10, 2021

I mean to the first script -- making artboards from layers. 

 

CarlosCanto
Community Expert
Community Expert
September 16, 2021

Hello, could someone make this script into a download, I seem to lack the know-how of creating a javascript documento to run this script from the source code.


Thanks!


Hi dario, copy the script posted here, open a text editor and paste the copied text. Save the file as "pickAgoodName.jsx", save it as Plain text (not Rich Text Format) anywhere you can easily find it.

 

to run in illustrator go to, File->Scripts->Other Scripts... and browse to where you saved the file

Disposition_Dev
Legend
February 28, 2017

Not sure exactly why yours didn't work, but that code was a little messy. it had multiple tests to determine whether a doc exists, and there are two loops where you could get away with one. Here's a snippet that i briefly tested and is working how you want, unless i misunderstand what you're asking for.

 

function container()

{

  if(app.documents.length > 0)

  {

    var doc = app.activeDocument;

    var layers = doc.layers

    var artboards = doc.artboards;

 

    for(var i=0;i<doc.layers.length;i++)

    {

      if(i>0)

      {

        var newAb = artboards.add([0,0,5,-5]); 

      }

      var thisLayer = doc.layers[i];

      thisLayer.hasSelectedArtwork = true;

      doc.fitArtboardToSelectedArt(i);

      artboards[i].name = layers.name;

      doc.selection = null;

    }

    alert("Finished:\nRenaming of Artboards to match Layer names is complete");

  }

  else

  {

    alert("No Open / Active Document Found");

  }

}

container();

 

 

azisher
azisherAuthor
Participating Frequently
March 1, 2017

Hi William, thank you for your answer, but its not what I want.

I want to reserve the script, from artboards to layers. Artboards >> Layers

Disposition_Dev
Legend
March 1, 2017

I think something is getting lost in translation here. I don't believe i'm understanding.

Are you saying that you want to create one layer for each artboard and set the layer name to match the artboard name?