Script to Toggle Group Visibility by Name (All Layers)

Community Beginner ,
Jan 31, 2022 Jan 31, 2022

Copy link to clipboard

Copied

Hello everyone,

 

I have a project that features a character with multiple facial features in labeled groups (glasses, hair wave, ect.). These same groups are present on every layer. To save on time I'm looking for a script that can toggle the visibility of a group by referncing its name. Sadly, I have very little experience with scripting myself and anything I've found online has possessed a syntax error.

 

Does anyone know of a script that can achieve this?Screen Shot 2022-01-31 at 5.45.13 PM.png

TOPICS
Draw and design , Scripting

Views

171

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 , Jan 31, 2022 Jan 31, 2022
the script you posted does exactly what you need, except it doesn't toggle all groups visibility on/off (meaning all on or all off). It toggles visibility per item. If an item is on, it will be turned off, but if some other item is off it will be turned on. is it not working on your end? try this version, it's the same script you posted, I just added a Window to enter the group name so you don't have to edit the script each time. Also, yes, if you start with all on, running the script will...

Likes

Translate

Translate
Adobe Community Professional ,
Jan 31, 2022 Jan 31, 2022

Copy link to clipboard

Copied

that's a very particular request, I doubt existing scripts would exists for that.

 

how would the toggle work? can you elaborate?

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 ,
Jan 31, 2022 Jan 31, 2022

Copy link to clipboard

Copied

At it's most basic, I think a script could run once to make all the similarly-named groups visible, and if run again it would toggle them to be off. Ideally, such a script could include a prompt for the name, but that's even more advanced.

 

Here's an example of a script I found online that comes close to the mark:

 

// jshint -W118
// globals app

// jshint ignore:start
#target illustrator
#targetengine main
// jshint ignore:end

function loop(flag, layers) {

    var groups, groupsCount;
    var subLayers, subLayersCount;
    var i1, i1l;
    var i2, i2l;

    // Loop over layers:
    for (i1 = 0, i1l = layers.length; i1 < i1l; i1++) {

        $.writeln('Layer: ', layers[i1].name);

        // Get layer groups:
        groups = layers[i1].groupItems;
        groupsCount = groups.length;

        // If there are nested groups:
        if (groupsCount) {

            for (i2 = 0, i2l = groups.length; i2 < i2l; i2++) {

                $.writeln('Group: ', groups[i2].name);

                // Does group match flag?
                if (groups[i2].name.toLowerCase() == flag) {

                    // Flag matched, so toggle hidden attribute:
                    groups[i2].hidden = ( ! groups[i2].hidden);

                }

            }

        }

        // Get layer layers:
        subLayers = layers[i1].layers;
        subLayersCount = layers.length;

        // If there are nested layers:
        if (subLayersCount) {

            // Call self and recurse:
            loop(flag, subLayers);

        }

    }

}

if (app.documents.length > 0) {

    // This script assumes you want to toggle visibility of “groups”
    // with a name that matches first argument passed below.
    loop(
        'featured product', // Group name used to toggle visibility.
        app.activeDocument.layers
    );

}
 

So using my own file as an example, I could change the script name to 'Wave' and Illustrator would know to change the visibility on any grouped object that shared that name.

 

I tried doing just that but Illustrator told me there was a Syntax Error on the first line. I don't know enough about coding to fix that.

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 ,
Jan 31, 2022 Jan 31, 2022

Copy link to clipboard

Copied

I'm not sure I'm doing a good job of explaining myself. This GIF comes from the same site where I found that code, but it's over five years old.

 

If the script registers the name "Featured Prdouct", it looks for any group with same name and toggles off the visibility. I don't know if running the script a second time would switch them back on.

hrjFA.gif

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 ,
Jan 31, 2022 Jan 31, 2022

Copy link to clipboard

Copied

the script you posted does exactly what you need, except it doesn't toggle all groups visibility on/off (meaning all on or all off). It toggles visibility per item. If an item is on, it will be turned off, but if some other item is off it will be turned on.

 

is it not working on your end?

 

try this version, it's the same script you posted, I just added a Window to enter the group name so you don't have to edit the script each time.

 

Also, yes, if you start with all on, running the script will turn them all off. Running the script a second time will turn them all on.

 

Group Name is Case Sensitive

 

// jshint -W118
// globals app

// jshint ignore:start
#target illustrator
// #targetengine main
// jshint ignore:end

function loop(flag, layers) {

    var groups, groupsCount;
    var subLayers, subLayersCount;
    var i1, i1l;
    var i2, i2l;

    // Loop over layers:
    for (i1 = 0, i1l = layers.length; i1 < i1l; i1++) {

        $.writeln('Layer: ', layers[i1].name);

        // Get layer groups:
        groups = layers[i1].groupItems;
        groupsCount = groups.length;

        // If there are nested groups:
        if (groupsCount) {

            for (i2 = 0, i2l = groups.length; i2 < i2l; i2++) {

                $.writeln('Group: ', groups[i2].name);

                // Does group match flag?
                if (groups[i2].name.toLowerCase() == flag) {

                    // Flag matched, so toggle hidden attribute:
                    groups[i2].hidden = (!groups[i2].hidden);

                }

            }

        }

        // Get layer layers:
        subLayers = layers[i1].layers;
        subLayersCount = layers.length;

        // If there are nested layers:
        if (subLayersCount) {

            // Call self and recurse:
            loop(flag, subLayers);

        }

    }

}

if (app.documents.length > 0) {

    // This script assumes you want to toggle visibility of “groups”
    // with a name that matches first argument passed below.
    loop(
        Window.prompt("Enter Name of Group to Toggle Visibility", "width", "Toggle Group Visibility"), // Group name used to toggle visibility.
        app.activeDocument.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
Advisor ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

Are you using "Group" and "Layer" to mean the same thing i.e. layer or sub-layer?

I don't know how to name a group! (and when I mean group I mean two or more objects grouped with ctrl-G...)

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 ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

You can name a group by double clicking on it in the Layers panel.

It looks like the script is looping through both groups and sublayers, so it shouldn't matter.

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
Advisor ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

Ah, on sub-layers, got it! Thanks.

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 ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

quote

Are you using "Group" and "Layer" to mean the same thing i.e. layer or sub-layer?

I don't know how to name a group! (and when I mean group I mean two or more objects grouped with ctrl-G...)


By @Met1

 

the script will toggle Group's visibility, not layers or sublayers. The script goes through all layers and sub-layers but only groups will be affected.

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 ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

I think what they are looking for is "includes", as so:

groups[i2].name.indexOf(flag) > -1

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 ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

quote

I think what they are looking for is "includes", as so:

groups[i2].name.indexOf(flag) > -1

By @Silly-V

 

right, it flags groups that includes part of the search string, it does not need to match the whole group's name

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 ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

Thank you for that updated script! I think I've done something wrong in copying it as I get the same error as I did with the older code.

 

Screen Shot 2022-02-01 at 11.03.11 AM.png

 

I'm on a Mac Pro and have been using Text Edit to paste the text into a file and then change the extension to .JSX. I think something in this process is created unwanted code. Is there a better program I should use?

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 ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

TextEdit defaults to Rich Text Format (RTF). You just need text (.txt) -- make sure you specify on saving, don't just change the file extension.

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 ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

LATEST

Ah, that did the trick and I'm happy to report the script works great! The only trick is that I misinterpeted the case sensitive aspect. If I write the group name all lower-case, it works great!

 

Thank you, everyone, for your insight and patience.

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