Skip to main content
Participating Frequently
October 14, 2022
Answered

Illustrator scripting help - Create new artboard per layer, make and place symbols

  • October 14, 2022
  • 1 reply
  • 2176 views

I am trying to automate a workflow process that I recently developed, focussed on improving the design process in illustrator using artboards but while still optimizing the transfer of artwork to after effects for animation.  My goal is to develop a script that will 1) create new artboards for each separate layer (not the sublayers) and also match the name of the newly created artboard to the referenced layer (not required but it would be nice).  Then make symbols for each layers artwork and place it on the newly created artboard.  And lastly create a new layer, called something like "symbols collection" where each of the layer symbols will be placed.  I am still pretty novice when it comes to scripting, but I am good enough as decoding existing scripts, so I am using William Campbell's repeat artboard as a basis with the first step, creating artboards for each layer.  I don't believe there is a method for setting the newly created artboard name though.  I can get the layer names, and I can get the artboard names, but I am not entirely sure how to make that connection between them.  Then there is the question of automating the symbol conversion.  This alone could be a huge time saver and possibly be enough to make the workflow automation sufficient.  If anyone has any scripts that relate, or suggestions for methods, I would very much appreciate any and all input I can get!  

This topic has been closed for replies.
Correct answer Charu Rajput

Looks like the illustrator file won't link so here is the drive link.

https://drive.google.com/file/d/1XbY5LzuY6Afb4oNL3XJUCYC8DEu01ZVv/view?usp=sharing


HI @Sethj78737,

Could you please try the following version?

var doc = app.activeDocument;
var gout = 10;
var dx, dy;
dx = dy = 0;
var ab = doc.artboards[0];
var _symbolsArray = new Array();

var _layers = doc.layers;
for (var l = 0; l < _layers.length; l++) {
    if (_layers[l].visible) {
        var rect = ab.artboardRect;
        var W = doc.width;
        var H = doc.height;
        dx = W + gout;

        //Create a new artboard
        var ab = doc.artboards.add([rect[0] + dx, rect[1], rect[0] + dx + W, rect[3]]);
        ab.name = _layers[l].name;  // Give same name ti artboard as layer
        var _pageItems = _layers[l].pageItems;
        app.executeMenuCommand('deselectall');
        for (var p = 0; p < _pageItems.length; p++) {
            _pageItems[p].position = new Array(ab.artboardRect[0] + 10, (ab.artboardRect[1]));
            // _pageItems[p].translate(ab.artboardRect[0] + 10, dy);
        }
        app.redraw();

        //Create symbol of items for new artboards
        if (_pageItems.length) {
            doc.artboards.setActiveArtboardIndex(l + 1);
            app.executeMenuCommand('selectallinartboard');
            app.executeMenuCommand('group');
            var _selectedItem = app.selection[0];
            var _symbol = doc.symbols.add(_selectedItem);
            var _placedSymbol = doc.symbolItems.add(_symbol);
            _placedSymbol.position = _selectedItem.position;
            _symbolsArray.push(_placedSymbol);
            //  _selectedItem.remove()  //Uncomment if you want to remove item from which symbol is created
        }
    }
}

//Move all placed symbols to new layer 'symbols collection'
var symbolLayer = doc.layers.add();
symbolLayer.name = 'symbols collection';
for (var s = 0; s < _symbolsArray.length; s++) {
    _symbolsArray[s].move(symbolLayer, ElementPlacement.PLACEATEND);
}
app.executeMenuCommand('deselectall');

1 reply

Charu Rajput
Community Expert
Community Expert
October 14, 2022

Hi @Sethj78737 , try following script. It may not be perfect version, as we can more validations to it. I ahve added a sample document as well on which I test the script.

var doc = app.activeDocument;
var gout = 10;
var dx, dy;
dx = dy = 0;
var ab = doc.artboards[0];
var _symbolsArray = new Array();

var _layers = doc.layers;
for (var l = 0; l < _layers.length; l++) {
    var rect = ab.artboardRect;
    var W = doc.width;
    var H = doc.height;
    dx = W + gout;

    //Create a new artboard
    var ab = doc.artboards.add([rect[0] + dx, rect[1], rect[0] + dx + W, rect[3]]);
    ab.name = _layers[l].name;  // Give same name ti artboard as layer
    var _pageItems = _layers[l].pageItems;
    app.executeMenuCommand('deselectall');
    for (var p = 0; p < _pageItems.length; p++) {
        _pageItems[p].translate(ab.artboardRect[0] + 10, dy);
    }
    app.redraw();

    //Create symbol of items for new artboards
    if (_pageItems.length) {
        doc.artboards.setActiveArtboardIndex(l + 1);
        app.executeMenuCommand('selectallinartboard');
        app.executeMenuCommand('group');
        var _selectedItem = app.selection[0];
        var _symbol = doc.symbols.add(_selectedItem);
        var _placedSymbol = doc.symbolItems.add(_symbol);
        _placedSymbol.position = _selectedItem.position;
        _symbolsArray.push(_placedSymbol);
        //  _selectedItem.remove()  //Uncomment if you want to remove item from which symbol is created
    }
}

//Move all placed symbols to new layer 'symbols collection'
var symbolLayer = doc.layers.add();
symbolLayer.name = 'symbols collection';
for (var s = 0; s < _symbolsArray.length; s++) {
    _symbolsArray[s].move(symbolLayer, ElementPlacement.PLACEATEND);
}
app.executeMenuCommand('deselectall');

 

Hope it helps and you can modify it as required.

 

Best regards
Participating Frequently
October 14, 2022

Hi Charu,

 

Thanks so much for sharing this attempt at the script...I was able to get the creation of the artboards per layer, so now I am going to try and convert the layer content to symbols.  I tried running yours and it errors at line #31 required value is missing.  Error 1242 - Illegal argument , var _symbol = doc.symbols.add(_selectedItem).  Thoughts on what might be causing this??

Charu Rajput
Community Expert
Community Expert
October 14, 2022

Is this error in the same document, that I have shared or some another document?

Or you can try by adding a line 

app.redraw();

before 

 var _selectedItem = app.selection[0];

 

Let me know how it goes.

Best regards