Skip to main content
jonohunt
Known Participant
August 29, 2018
Answered

Modify script to select hidden layers/groups?

  • August 29, 2018
  • 2 replies
  • 2653 views

I found a script to select the previous layer and set it as visible. It works but I'd like to modify it slightly if possible.

At the moment if you start from the selected layer like so.

I'd like it so that each time the script is run it selects the previous layer/group and make it shown/visible like this.

But what currently happens with the script is that when it gets to a group is expands the folder and starts selecting layers inside the folder and turning on visibility.

Does anyone know how to modify the script (or use a different one) so that for groups it just turns the visibility on without expanding the group and selecting layers inside the groups?

Here's the current code:

// Select previous layer/group down & set to visible

var doc = app.activeDocument;

var cLayer = doc.activeLayer;

function activ (nLayer_f)

{

  var check = nLayer_f.visible;

  doc.activeLayer = nLayer_f;

  if (check == false)

  doc.activeLayer.visible = false;

}

var parL = doc.activeLayer.parent;

var parLln = parL.layers.length;

for(i=0; i<parLln;)

{

  if(parL.layers==cLayer)

  {

    try

    {

      nLayer = cLayer.layers[0];

    }

      catch(e)

      {

        if(i!=parLln-1)

        {

          nLayer=parL.layers[i+1];

        }

        else

        {

          upmem = parL;

          while (upmem!=doc && upmem.parent.layers[upmem.parent.layers.length-1]==upmem)

          {

            upmem = upmem.parent;

          }

          if (upmem==doc)

          {

            upmem=upmem.layers[0];

            var lastmem = 1;

          }

          for(k=0;k<upmem.parent.layers.length;)

          {

            if(upmem.parent.layers==upmem)

            {

              aa=k;

              if(lastmem==1)

              {aa=-1;}

              nLayer=upmem.parent.layers[aa+1];

              k=upmem.parent.layers.length;

            }

            else {k++;}

          }

        }

      }

      activ (nLayer);

      i=parLln-1;

    }

    i++;

}

// Show current layer

app.activeDocument.activeLayer.visible = true;

This topic has been closed for replies.
Correct answer r-bin

Change

try 

{

  nLayer = cLayer.layers[0]; 

to

try 

{

  throw(0)

  nLayer = cLayer.layers[0]; 

2 replies

r-binCorrect answer
Legend
August 29, 2018

Change

try 

{

  nLayer = cLayer.layers[0]; 

to

try 

{

  throw(0)

  nLayer = cLayer.layers[0]; 

jonohunt
jonohuntAuthor
Known Participant
August 29, 2018

Thanks, that works!

It does expand the folders as it sets them as visible (a bit annoying but I can live with that), but doesn't move into the folders, which what I wanted

Kukurykus
Legend
August 29, 2018

I've tried both scripts and neither do anything for me.

Should I run them on their own, or add the code to my existing code?

Here's the full code, in case it helps to see what I'm doing:

// Set current layer as visible

app.activeDocument.activeLayer.visible = true;

// Save as PNG to ~/Desktop appending Layer Name

var doc = app.activeDocument;

var docBaseName = app.activeDocument.name.slice(0,-13);

var docDimensions = app.activeDocument.name.slice(-13,-4);

var layerName = app.activeDocument.activeLayer.name

var saveFile = new File("~/Desktop/" + docBaseName + "." + layerName + docDimensions + ".png");

pngSaveOptions = new PNGSaveOptions();

pngSaveOptions.interlaced = false;

doc.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

// Hide current layer

app.activeDocument.activeLayer.visible = false;

// Select Previous Layer Down

var doc = app.activeDocument;

var cLayer = doc.activeLayer;

function activ (nLayer_f)

{

  var check = nLayer_f.visible;

  doc.activeLayer = nLayer_f;

  if (check == false)

  doc.activeLayer.visible = false;

}

  var parL = doc.activeLayer.parent;

  var parLln = parL.layers.length;

  for(i=0; i<parLln;)

  {

    if(parL.layers==cLayer)

    {

      try

      {

        throw(0)

        nLayer = cLayer.layers[0];

      }

      catch(e)

      {

        if(i!=parLln-1)

        {

          nLayer=parL.layers[i+1];

        }

        else

        {

          upmem = parL;

          while (upmem!=doc && upmem.parent.layers[upmem.parent.layers.length-1]==upmem)

          {

            upmem = upmem.parent;

          }

          if (upmem==doc)

          {

            upmem=upmem.layers[0];

            var lastmem = 1;

          }

          for(k=0;k<upmem.parent.layers.length;)

          {

            if(upmem.parent.layers==upmem)

            {

              aa=k;

              if(lastmem==1)

              {aa=-1;}

              nLayer=upmem.parent.layers[aa+1];

              k=upmem.parent.layers.length;

            }

            else {k++;}

          }

        }

      }

      activ (nLayer);

      i=parLln-1;

    }

    i++;

}


Here's corrected code:

len = (lrs = (lyr = (aD = activeDocument).layers[0]).layers).length

for(i = 0; i < len; i++) {

     if (lrs == aD.activeLayer) {

          if (i == len - 1) aD.activeLayer = lrs[0]

          else {aD.activeLayer = lrs[i + 1]} break

     }

}

According to described scenario it works while you are inside of main group on some first level layer(Set).

btw first code that uses AM needs folders you are selecting (& making visible) by a script to be collapsed.

Answering your question they work independly so without a script you originally posted. But once again they were wrong as I forgot about 'Artwork' so if you want to use something instead on the top of this post you find correctly working script.

Kukurykus
Legend
August 29, 2018

len = (lrs = (aD = activeDocument).layers).length

for(i = 0; i < len; i++) {

     if (lrs == aD.activeLayer) {

          if (i != len - 1) {

               lrs[i + 1].visible = true

          }

          else lrs[0].visible = true

          function sTT(v) {return stringIDToTypeID(v)}

          (ref = new ActionReference()).putEnumerated

          (sTT('layer'), sTT('ordinal'), sTT('backwardEnum'));

          (dsc = new ActionDescriptor()).putReference(sTT('null'), ref)

          dsc.putBoolean(sTT('makeVisible'), false);

          executeAction(sTT('select'), dsc); break

     }

}

Legend
August 29, 2018

You can make your code even shorter.

sTT = stringIDToTypeID;