Script to Rename Artboards Based on Layers (and Reverse)

Community Beginner ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

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
TOPICS
Scripting

Views

1.8K

Likes

Translate

Translate

Report

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

correct answers 1 Correct answer

Adobe Community Professional , Jul 31, 2017 Jul 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)    {        //c...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

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'

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

Glad to help. Good luck. 😃

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

i want a fix name no show enter name option box

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

Would ya look at what a couple of years here in the forums can do! This code is that of a seasoned veteran

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

Well thanks, V. That means a lot coming from you.

I have to give all the credit to the brilliant and helpful minds on this forum. And a special shoutout to someone who will never read this comment, Daniel Shiffman of NYU and The Coding Train (on youtube), for the significantly more efficient and cleaner intersection detection method.

Even though nobody asked, i'll elaborate. In a previous version of one of my flagship scripts here at work, i used to use this very long winded and confusing method of checking for overlap:

function intersect(art, dest) {

            var intersect = false;

            var artBounds = art.geometricBounds;

            var destBounds = dest.geometricBounds;

            var AL = artBounds[0];

            var AT = artBounds[1];

            var AR = artBounds[2];

            var AB = artBounds[3];

            var DL = destBounds[0];

            var DT = destBounds[1];

            var DR = destBounds[2];

            var DB = destBounds[3];

            //check for intersection of 1 or 2 sides

            //this part breaks down if 3 sides are intersected

            if (((AL >= DL && AL <= DR) || (AR >= DL && AR <= DR)) && ((AT <= DT && AT >= DB) || (AB <= DT && AB >= DB)))

            //this art intersects this destination on one or two sides

                intersect = true;

            else if ((((AL <= DR && AL >= DL) || (AL <= DL && AR >= DL)) && (AT >= DT && AB <= DB)) || (((AB <= DT && AB >= DB) || (AT >= DB && AT <= DT)) && AL <= DL && AR >= DR)) {

                //art covers 3 sides of dest piece

                intersect = true;

            } else if (AT >= DT && AR >= DR && AB <= DB && AL <= DL) {

                //art completely covers dest piece

                intersect = true;

            }

            return intersect;

        }

Daniel, in one of his many many brilliant youtube videos, pointed out that you can save yourself tons of calculations by simply looking for a single condition that would make an overlap impossible (like the right edge of something being further left than the left edge of the potential dest), rather than looking for a series of conditions that all need to be true to achieve a true result.

So this concludes the unsolicited presentation about efficient overlap detection.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 23, 2022 Mar 23, 2022

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 23, 2022 Mar 23, 2022

Copy link to clipboard

Copied

Hi Andy, sorry but this is an Illustrator Script, it won't work in Photoshop.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

Hello Andy.

 

Sorry, i must have missed this message and didn't see it till today. As Carlos mentioned, this script is written for illustrator and won't work in photoshop. I'm struggling to think of what you might need it for in photoshop though. As far as i know, photoshop doesn't even have the concept of artboards. 

 

What is it that you're trying to do in photoshop with this script? Maybe it can be ported.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

Download full resolution images
Available until Apr 28, 2022

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

LATEST

I've sent you a small file which is representative of what I'm talking about.

Thanks so much.

Likes

Translate

Translate

Report

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