2

I want to toggle the visibility of the sublayers with a certain name. Now, I've seen it done for a layer:

#target illustrator all_layers = app.activeDocument.layers.getByName("Layer 8") all_layers.visible = ! all_layers.visible

Here is how I reference a sublayer, but I cannot connect the dots.

How to reference a sublayer by name in an Illustrator script?

Thanks!

  • There really no such thing as a sub layer are you talking about page items or do you mean nested layer objects? (yes i know the panel is named layer panel but on the whole 90% of what you see in it is not a layer, but a object) – joojaa Jan 7 '17 at 16:17
  • This is what I have in my doc !Ai Screenshot. I would like to rename "Featured Products" to smth else – D.Montana Jan 7 '17 at 16:22
  • Sorry, to toggle visibility... I wrote rename. – D.Montana Jan 7 '17 at 16:28
  • Yes but its important to know if those are groups or actual layer objects! Doubleclick on entry and see what the options panel says. If it says layer then those are layers if it says just options then its a group. – joojaa Jan 7 '17 at 16:30
  • 2
    Like i suspected these are not layers – joojaa Jan 7 '17 at 17:01
4

Considering that you are working with groups (and not nested layers), the below script will toggle the “hidden” attribute of groups within layers (and nested layers):

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

}

Test file: test.ai

Note that I didn’t take into account nested layers with the name of “featured product”, but (not having given this scenario too much thought) you could simply add a condition to toggle the layer visibility for that layer instead of recursing into it's contents.

Ideas for a slight improvements to the code:

  • Prompt the user to input a string as the target group name to toggle
  • Include an option to show/hide layers in that same prompt window (as a checkbox perhaps?)
  • Wrap code in IIFE plus general house cleaning of syntax

Be warned that I have not stress-tested the above code, so caveat emptor. :)

|improve this answer|||||
  • Sure thing! Thanks for the √. Let me know if you have any questions. – mhulse Jan 20 '17 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.