Skip to main content
sensibleworld
Participating Frequently
July 31, 2017
Answered

Script to Rename Artboards Based on Layers (and Reverse)

  • July 31, 2017
  • 2 replies
  • 3307 views

A couple of years ago I had some awesome help making a couple of scripts to rename layers based on the containing artboard (Re: Script to Rename Artboards with Layer Names). One problem posed (and solved) was:

- Each artboard has a name.

- Each layer NEEDS a name.

- There are an equal number of layers and artboards.

- Only one layer "exists" on each artboard. (i.e. They share coordinates.)

- I would like to name the layer with the same name as its encompassing artboard.

The solution was:

function artboardLayerNameMatch() { 

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

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

    } else { 

        var doc, i, l, ab, sel, n; 

        doc = app.activeDocument; 

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

            ab = doc.artboards

            doc.artboards.setActiveArtboardIndex(i); 

            doc.selectObjectsOnActiveArtboard(); 

            sel = doc.selection[0]; 

            sel.parent.name = ab.name; 

            doc.selection = false; 

        } 

    } 

artboardLayerNameMatch();

Now, I'm wondering if the reverse is also possible?

The new scenario is:

  • Each layer has a name.
  • Each artboard NEEDS a name.
  • There are an equal number of layers and artboards.
  • Only one layer "exists" on each artboard. (i.e. They share coordinates.)
  • I would like to name the artboard with the same name as the layer that resides “on” it
This topic has been closed for replies.
Correct answer Disposition_Dev

Here you go. This is certainly not nearly as robust as it could be, but it gets the job done.

function container()

{

    var docRef = app.activeDocument;

    var layers = docRef.layers;

    var aB = docRef.artboards;

    var curLay, curAb, rect, sel, bounds, aBounds, intersect = false;

    var aBLen = aB.length,

        layLen = layers.length;

    function isIntersect(sel, ab)

    {

        return !(sel.l > ab.r || sel.r < ab.l || sel.t < ab.b || sel.b > ab.t);

    }

    if (aBLen === layLen)

    {

        //clear out the selection

        docRef.selection = null;

        //loop the layers

        for (var x = 0; x < layLen; x++)

        {

            curLay = layers;

            curLay.hasSelectedArtwork = true;

            sel = docRef.selection[0];

            bounds = {

                l: sel.left,

                t: sel.top,

                r: sel.left + sel.width,

                b: sel.top - sel.height

            };

            for (var y = 0; y < aBLen && !intersect; y++)

            {

                curAb = aB;

                rect = curAb.artboardRect;

                aBounds = {

                    l: rect[0],

                    t: rect[1],

                    r: rect[2],

                    b: rect[3]

                };

                if (isIntersect(bounds, aBounds))

                {

                    intersect = true;

                    curAb.name = curLay.name;

                }

            }

            intersect = false;

            docRef.selection = null;

        }

    }

    else

    {

        alert("You need to have a matching number of artboards and layers.");

    }

}

container();

2 replies

Disposition_Dev
Disposition_DevCorrect answer
Legend
July 31, 2017

Here you go. This is certainly not nearly as robust as it could be, but it gets the job done.

function container()

{

    var docRef = app.activeDocument;

    var layers = docRef.layers;

    var aB = docRef.artboards;

    var curLay, curAb, rect, sel, bounds, aBounds, intersect = false;

    var aBLen = aB.length,

        layLen = layers.length;

    function isIntersect(sel, ab)

    {

        return !(sel.l > ab.r || sel.r < ab.l || sel.t < ab.b || sel.b > ab.t);

    }

    if (aBLen === layLen)

    {

        //clear out the selection

        docRef.selection = null;

        //loop the layers

        for (var x = 0; x < layLen; x++)

        {

            curLay = layers;

            curLay.hasSelectedArtwork = true;

            sel = docRef.selection[0];

            bounds = {

                l: sel.left,

                t: sel.top,

                r: sel.left + sel.width,

                b: sel.top - sel.height

            };

            for (var y = 0; y < aBLen && !intersect; y++)

            {

                curAb = aB;

                rect = curAb.artboardRect;

                aBounds = {

                    l: rect[0],

                    t: rect[1],

                    r: rect[2],

                    b: rect[3]

                };

                if (isIntersect(bounds, aBounds))

                {

                    intersect = true;

                    curAb.name = curLay.name;

                }

            }

            intersect = false;

            docRef.selection = null;

        }

    }

    else

    {

        alert("You need to have a matching number of artboards and layers.");

    }

}

container();

sensibleworld
Participating Frequently
July 31, 2017

Works like a charm! Wow. Thank you so much, williamadowling​!

Disposition_Dev
Legend
July 31, 2017

Glad to help. Good luck. =)

Disposition_Dev
Legend
July 31, 2017

Here's the inelegant, not very efficient pseudo-code. real code to follow:

if # of layers == # of artboards

     for each layer

          select all artwork on current layer

          bounds = visibleBounds of first item in selection array

          for each artboard

               if bounds +/- buffer overlaps current artboard

                    current artboard.name = current layer.name

                    break

else

     alert 'there must be an equal number of artboards and layers'