Skip to main content
Inspiring
March 17, 2014
Question

algorithm check

  • March 17, 2014
  • 1 reply
  • 580 views

I am intersted in developing a script which will allow the user to target any layer from the document's layer panel and turn its visibility on/off with a keyboard shortcut. Can someone plase check the logic of the algorithm. I plan on executing the script using the if else and for loop. I am not sure if there is a better way to achive the script bhavior.

many thaks

Description:

The script checks the layer panel for a sepcific layer name and

toggles the visibility of the layer name on/off using a keyboard shortcut.

The script tests for the presence of at least one layer in adition of the background layer and

alerts the user when the ducument does not have enough layers.

The script alerts the user if the name of the layer is incorrect.

Algorithm:

1-Test for document with atleast one art layer

2-Alert user if document does not contain any art layers

3-Decalre variable for artLayerName

4-Decalre for loop condition for artLayerName

5-Pass artlayerName varaible to for loop condition

6-Cycle through document layers

7-If for loop condition true

8-Toggle artLayerName on/off

9-If for loop condition false alert user: artLayerName is not present on layer panel

This topic has been closed for replies.

1 reply

pixxxelschubser
Community Expert
Community Expert
March 17, 2014

Hmmh?

do you really need a loop through all layers?

Is your layer hierarchy so complicated?

Please show a screenshot of your layers panel.

Or perhaps this simple example is already good enough for you?

// http://forums.adobe.com/thread/1430070?tstart=0

// LayerShowOrHideByName.jsx

// regards pixxxelschubser

var aLayName = prompt ("Layer name?", 'Your Layer')

showOrHideLayer ( aLayName );

function showOrHideLayer ( aLayName ) {

    try {

        aLay = app.activeDocument.layers.getByName(aLayName);

        if (aLay.visible == true) {

            aLay.visible = false;

            } else {

                aLay.visible = true;

                }

        } catch (e) {

            alert ("Cannot find this layer: " + aLayName);

        }

    }

But be sure: insert the layer name manuelly is very slow. You can better and faster go to the your layer by right clicking directly (while Move tool is active) in the picture and click on the layer name.

ojodegatoAuthor
Inspiring
March 18, 2014

Thank you for responding. Actually, I am not sure if I need to loop through layers is necessary. I was thinking that cycling through the layers was necessary to find a specific layer name.

What is necessary is to be able to detect a specific layer name and then turn its visibility on/off several times. Next select a different layer and turn its visibility on/off several tiems.

The prompt dialog is a nice feature but it pops up every time the script runs. This promp should popup once to specify the layer name and then not popup again until it is time to change to a different layer.

Question:

Can this be achieved with two scripts which are linked?

Script 1: Prompts for the layer name and passes the layer name to Script 2.

Script 2: Toggles the visibilty the layer entered in Script 1.

Chuck Uebele
Community Expert
Community Expert
March 18, 2014

pixxxel schubser's code is good for what you want to do, unless your layer is in a group.  Then the getByName will not work on those layers.  As to the two scripts, you can do this by writing the layer name to a csv or xml file that the other script can read and use until changed.

So you would add this code to pixxxel schubser's to write the name to a file

file = new File(fileName);//insert name and path

   

            file.encoding = 'UTF8';

            file.open('w');

            file.write(aLayName);

            file.close();