Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Modify script to select hidden layers/groups?

Explorer ,
Aug 29, 2018 Aug 29, 2018

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.

photoshop_script_select_previous_layer_1.png

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.

photoshop_script_select_previous_layer_2.png

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.

photoshop_script_select_previous_layer_3.png

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;

TOPICS
Actions and scripting
2.6K
Translate
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

People's Champ , Aug 29, 2018 Aug 29, 2018

Change

try 

{

  nLayer = cLayer.layers[0]; 

to

try 

{

  throw(0)

  nLayer = cLayer.layers[0]; 

Translate
Adobe
LEGEND ,
Aug 29, 2018 Aug 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

     }

}

Translate
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
People's Champ ,
Aug 29, 2018 Aug 29, 2018

You can make your code even shorter.

sTT = stringIDToTypeID;

Translate
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
Explorer ,
Aug 29, 2018 Aug 29, 2018

I tried the code on it's own but nothing seems to happen.

Should it be used on it's own, or added into the existing script? (Or replace just part of the existing script?)

Translate
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
People's Champ ,
Aug 29, 2018 Aug 29, 2018

Change

try 

{

  nLayer = cLayer.layers[0]; 

to

try 

{

  throw(0)

  nLayer = cLayer.layers[0]; 

Translate
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
Explorer ,
Aug 29, 2018 Aug 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

Translate
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
People's Champ ,
Aug 29, 2018 Aug 29, 2018

Expanded or not does not matter

Translate
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
LEGEND ,
Aug 29, 2018 Aug 29, 2018

Yes you are right, but I'm not sure it will always work as never tried. I'll see on next scripts, thx

Translate
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
LEGEND ,
Aug 29, 2018 Aug 29, 2018

The code of me doesn't expand folders and do the same

Translate
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
Explorer ,
Aug 29, 2018 Aug 29, 2018

Strange, doesn't do anything for me

Translate
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
LEGEND ,
Aug 29, 2018 Aug 29, 2018

What doesn't do anything for you? Ah you probably mean of my code. Yes you are right, I forgot about Artwork folder

btw if you don't care subfolders are beeing expanded despite of that you asked in your original post you can use also this:

len = (lrs = (aD = activeDocument).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

     }

}

Translate
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
Explorer ,
Aug 29, 2018 Aug 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++;

}

Translate
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
LEGEND ,
Aug 29, 2018 Aug 29, 2018

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.

Translate
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
Explorer ,
Aug 29, 2018 Aug 29, 2018
LATEST

Ah, cool it works now

Translate
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
People's Champ ,
Aug 29, 2018 Aug 29, 2018

del

Translate
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