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();

Participant
March 24, 2022

DilliamWowling

This script sounds exactly like what I need. I copied your code into a TextEdit file, converted it to plain text, and saved it into my Photoshop 2022 scripts folder as a .jsx file. Then I rebooted Photoshop, saw that the script name I gave it (LayerNameToArtboardName) was showing up in the scripts menu. Alas when I tried to run it I got an error message: Error 8: Syntax error... 

I'm not a scripter, or a programmer. The most I could ever call myself is a power user. I don't have the kind of rigorously exacting thought process that makes what people like you do possible for me to do. I do however have a large, tedious workflow in need of automation and would greatly appreciate whatever help you have time to give me.

Thanks very much.

Disposition_Dev
Legend
March 29, 2022

Hello Dilliam,

Photoshop does indeed have an artboards feature, which I find highly useful, indeed essential to my work. I use artboards to batch export large numbers of embedded PSD files from a single central PSD file into multiple jpegs in two banner sizes. I'm always looking for ways to improve my workflow. The exported jpeg filenames are based on a combination of the central PSD file name and the name of the individual artboards. The individual artboard names in turn are based on the filename of the embedded PSD files. What I was looking for was a way to remove the tedious step of manually naming the artboards (which sometimes number as high as 80-90) which I currently accomplish by copy-pasting part of the embedded PSD filename -- a problem you apparently solved with your Illustrator script. 

Regards,

Andy


can you share a sample file for me to test with? also include some examples of what the correct artboard name formatting is.

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'