Skip to main content
Participant
April 20, 2016
Answered

Checking if layer is visible and run a function depending on answer

  • April 20, 2016
  • 1 reply
  • 2802 views

Hey guys,

I've got a question that's been racking my brain the last few days.

So I want to run a script when I press a button that will check if a specific layer is visible. If yes, it will execute another function that shows additional layers. If the specific layer is not visible, it checks the next layer to see if that is visible. This continues until it checks all required layers and finds a visible layer.

Here's the script in question:

#target photoshop

//

// clthTopTt.jsx

//

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

//

//==================== actnStd ==============

//

function checkactnStd() {

  // Select

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "actnStd");

    desc1.putReference(cTID('null'), ref1);

    desc1.putBoolean(cTID('MkVs'), false);

    var list1 = new ActionList();

    list1.putInteger(17011);

    desc1.putList(cTID('LyrI'), list1);

    executeAction(cTID('slct'), desc1, dialogMode);

  };

  // If

  function step2(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putEnumerated(cTID('null'), cTID('Cndt'), cTID('Vsbl'));

    var ref1 = new ActionReference();

    ref1.putName(cTID('Actn'), "select actnStd");     <------- If the actnStd layer is visible, instead of running this action, it runs the selectactnStd() function.

    ref1.putName(cTID('ASet'), "clthTopTt");

    desc1.putReference(cTID('then'), ref1);

    var ref2 = new ActionReference();

    ref2.putName(cTID('Actn'), "check actnWlk");  <------- If the actnStd layer is NOT visible, it runs the checks the next layer (via the checkactnWlk() function.

    ref2.putName(cTID('ASet'), "clthTopTt");

    desc1.putReference(cTID('else'), ref2);

    executeAction(sTID('conditional'), desc1, dialogMode);

  };

  step1();      // Select

  step2();      // If

};

function selectactnStd() {

  // Show

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var list1 = new ActionList();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "topStdTt");

    ref1.putName(cTID('Lyr '), "topMskStd");

    list1.putReference(ref1);

    desc1.putList(cTID('null'), list1);

    executeAction(cTID('Shw '), desc1, dialogMode);

  };

  // Hide

  function step2(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var list1 = new ActionList();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "topWlkTt");

    ref1.putName(cTID('Lyr '), "topMskWlk");

    ref1.putName(cTID('Lyr '), "topRunTt");

    ref1.putName(cTID('Lyr '), "topMskRun");

    list1.putReference(ref1);

    desc1.putList(cTID('null'), list1);

    executeAction(cTID('Hd  '), desc1, dialogMode);

  };

  step1();      // Show

  step2();      // Hide

};

//

//==================== actnWlk ==============

//

function checkactnWlk() {

  // Select

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "actnWlk");

    desc1.putReference(cTID('null'), ref1);

    desc1.putBoolean(cTID('MkVs'), false);

    var list1 = new ActionList();

    list1.putInteger(17012);

    desc1.putList(cTID('LyrI'), list1);

    executeAction(cTID('slct'), desc1, dialogMode);

  };

  // If

  function step2(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putEnumerated(cTID('null'), cTID('Cndt'), cTID('Vsbl'));

    var ref1 = new ActionReference();

    ref1.putName(cTID('Actn'), "select actnWlk");

    ref1.putName(cTID('ASet'), "clthTopTt");

    desc1.putReference(cTID('then'), ref1);

    var ref2 = new ActionReference();

    ref2.putName(cTID('Actn'), "check actnRun");

    ref2.putName(cTID('ASet'), "clthTopTt");

    desc1.putReference(cTID('else'), ref2);

    executeAction(sTID('conditional'), desc1, dialogMode);

  };

  step1();      // Select

  step2();      // If

};

function selectactnWlk() {

  // Show

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var list1 = new ActionList();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "topWlkTt");

    ref1.putName(cTID('Lyr '), "topMskWlk");

    list1.putReference(ref1);

    desc1.putList(cTID('null'), list1);

    executeAction(cTID('Shw '), desc1, dialogMode);

  };

  // Hide

  function step2(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var list1 = new ActionList();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "topStdTt");

    ref1.putName(cTID('Lyr '), "topMskStd");

    ref1.putName(cTID('Lyr '), "topRunTt");

    ref1.putName(cTID('Lyr '), "topMskRun");

    list1.putReference(ref1);

    desc1.putList(cTID('null'), list1);

    executeAction(cTID('Hd  '), desc1, dialogMode);

  };

  step1();      // Show

  step2();      // Hide

};

//

//==================== actnRun ==============

//

function checkactnRun() {

  // Select

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "actnRun");

    desc1.putReference(cTID('null'), ref1);

    desc1.putBoolean(cTID('MkVs'), false);

    var list1 = new ActionList();

    list1.putInteger(17013);

    desc1.putList(cTID('LyrI'), list1);

    executeAction(cTID('slct'), desc1, dialogMode);

  };

  // If

  function step2(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putEnumerated(cTID('null'), cTID('Cndt'), cTID('Vsbl'));

    var ref1 = new ActionReference();

    ref1.putName(cTID('Actn'), "select actnRun");

    ref1.putName(cTID('ASet'), "clthTopTt");

    desc1.putReference(cTID('then'), ref1);

    var ref2 = new ActionReference();

    ref2.putName(cTID('Actn'), "check actnAc");

    ref2.putName(cTID('ASet'), "clthTopTt");

    desc1.putReference(cTID('else'), ref2);

    executeAction(sTID('conditional'), desc1, dialogMode);

  };

  step1();      // Select

  step2();      // If

};

function selectactnRun() {

  // Show

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var list1 = new ActionList();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "topRunTt");

    ref1.putName(cTID('Lyr '), "topMskRun");

    list1.putReference(ref1);

    desc1.putList(cTID('null'), list1);

    executeAction(cTID('Shw '), desc1, dialogMode);

  };

  // Hide

  function step2(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var list1 = new ActionList();

    var ref1 = new ActionReference();

    ref1.putName(cTID('Lyr '), "topStdTt");

    ref1.putName(cTID('Lyr '), "topMskStd");

    ref1.putName(cTID('Lyr '), "topWlkTt");

    ref1.putName(cTID('Lyr '), "topMskWlk");

    list1.putReference(ref1);

    desc1.putList(cTID('null'), list1);

    executeAction(cTID('Hd  '), desc1, dialogMode);

  };

  step1();      // Show

  step2();      // Hide

};

After it checks the last layer, it then ends the script.

"clthTopTtLoop.jsx"

// EOF

Any help is seriously appreciated. Thanks guys!

This topic has been closed for replies.
Correct answer Davmi

Thanks again natrev.

I haven't tried the code yet, but I ended up hiring a JavaScript Developer. He said my code was generic recorded code, so he'll clean it up and resend it to me. I'll post a response with a version of the code that will help others in the future with this issue. I'll also try your code just to get more familiar with it.

Again, I really appreciate all your help.

-Dave


Hey guys, I promised I'd update this post to help others if I got the answer, so here goes:

First, the developer created a function to automatically get the layer. I was having problems figuring out how to select layers, particularly because the layers were nested in groups within groups. This helped resolve that. The function was this:

function selectLayer(name) {

  var desc = new ActionDescriptor();

  var ref = new ActionReference();

  ref.putName( charIDToTypeID('Lyr '), name );

  desc.putReference( charIDToTypeID('null'), ref );

  desc.putBoolean( charIDToTypeID('MkVs'), false );

  executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );

  return

};

You then call the layer by using the actual layer name (assuming your layer names are unique. If they are not, you'll only end up selecting the first layer with that specific name) throughout your script file.

IE. In my case, selectLayer("actnStd"); or selectLayer("actnWlk");

You then set the visibility using the following code:

// show

app.activeDocument.activeLayer.visible = true;

// hide

app.activeDocument.activeLayer.visible = false;

You then use a function to check if a layer is visible. If the layer is visible, you execute the function that shows/hides the respective layers:

function CheckactnWlk() {

  selectLayer("actnWlk");

  if  (app.activeDocument.activeLayer.visible == true) {

  SelectactnWlk()

  }

  else  {

  CheckactnRun();

  }  

};

So in the end, the final script looks like this:

#target photoshop

app.bringToFront();

CheckactnStd();

function CheckactnStd() {

  selectLayer("actnStd");

  if  (app.activeDocument.activeLayer.visible == true) {

  SelectactnStd()

  }

  else  {

  CheckactnWlk();

  }

};

  

function SelectactnStd() {  

  selectLayer("topMskStd");

  app.activeDocument.activeLayer.visible = true;

  selectLayer("topStdTt");

  app.activeDocument.activeLayer.visible = true;

  selectLayer("topWlkTt");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topMskWlk");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topRunTt");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topMskRun");

  app.activeDocument.activeLayer.visible = false;

};

  

function CheckactnWlk() {

  selectLayer("actnWlk");

  if  (app.activeDocument.activeLayer.visible == true) {

  SelectactnWlk()

  }

  else  {

  CheckactnRun();

  }  

};  

    

function SelectactnWlk() {

  selectLayer("topMskStd");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topStdTt");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topWlkTt");

  app.activeDocument.activeLayer.visible = true;

  selectLayer("topMskWlk");

  app.activeDocument.activeLayer.visible = true;

  selectLayer("topRunTt");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topMskRun");

  app.activeDocument.activeLayer.visible = false;

   

};

  

function CheckactnRun() {

  selectLayer("actnRun");

  if  (app.activeDocument.activeLayer.visible == true) {

  SelectactnRun()

  }

  else  {

  alert ('There is no Selected Layer', 'Please select a layer', 0);   

  }  

};

function SelectactnRun() {

  selectLayer("topMskStd");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topStdTt");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topWlkTt");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topMskWlk");

  app.activeDocument.activeLayer.visible = false;

  selectLayer("topRunTt");

  app.activeDocument.activeLayer.visible = true;

  selectLayer("topMskRun");

  app.activeDocument.activeLayer.visible = true;

};

function selectLayer(name) {

        var desc = new ActionDescriptor();

        var ref = new ActionReference();

        ref.putName( charIDToTypeID('Lyr '), name );

       desc.putReference( charIDToTypeID('null'), ref );

       desc.putBoolean( charIDToTypeID('MkVs'), false );

       executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );

       return

};

app.preferences.rulerUnits = rUnit;

app.preferences.typeUnits = tUnit;

selectLayer("actnStd");

Good luck guys! Now on to figuring out how to select random layers via script.

1 reply

natrev
Legend
April 20, 2016

Hi Davmi,

How do you identify the specific layer? any Special name or suffix / prefix / layer index no / position

The below code is working whether if any layer visible execute the action "selectactnStd"

else execute the action "checkactnWlk" action

Code..

#target photoshop

if ( app.documents.length <= 0 ) alert( "No Image in Use" );

else{main();}

function main(){

    var docRef = app.activeDocument;

    var myLayers=docRef.layers;

  

    for(i=0; i<myLayers.length;i++){

        if(myLayers.typename=="ArtLayer" && myLayers.visible==true && myLayers.name!="Background"){

            alert("Layer is Visible");

            docRef.activeLayer=myLayers;

            app.doAction ('selectactnStd', 'Set 1'); //Action name and Action set or Action group name

            }

        else if (myLayers.typename=="ArtLayer" && myLayers.visible==false && myLayers.name!="Background"){

            alert("Layer is inVisible");

            docRef.activeLayer=myLayers;

            app.doAction ('checkactnWlk', 'Set 1');//Action name and Action set or Action group name

            myLayers.visible=false;

        }

    }

}

-yajiv

DavmiAuthor
Participant
April 20, 2016

Thank you so much for the reply, natrev.

To give a little more background on what I'm trying to achieve:

So right now as it stands, the script IS working that I posted my first post. The issue here is that I'd like to use functions instead of actions. It might seem like a hassle, but I'd like to circumvent my friends from having to download/rely on addition action (.atn) files if possible.

My current script works like this at the moment:

Select the layer named actnStd            (this is done with the checkactnStd() function).

  • If layer is visible, run the action to select set "clthTopTt" and action "select actnStd")
  • If layer is not visible, run the action to select set "clthTopTt" and action "check actnWlk") (which checks the next layer for visibility).

Select the layer named actnWlk           (this is done with the checkactnWlk() function).

  • If layer is visible, run the action to select set "clthTopTt" and action "select actnWlk")
  • If layer is not visible, run the action to select set "clthTopTt" and action "check actnRun") (which checks the next layer for visibility).

Select the layer named actnRun          (this is done with the checkactnRun(); function).

  • If layer is visible, run the action to select set "clthTopTt" and action "select actnRun")
  • If layer is not visible for any of the above, then stop the loop.

This is the script that I want:

Select the layer named actnStd        (this is done with the checkactnStd() function).

  • If layer is visible, run the function selectactnStd();     (This will basically show the layers I want and hide the layers I don't want and end the loop).
  • If layer is not visible, run the function checkactnWlk();      (This will move on to the next layer to check for visibilty).

Select the layer named actnWlk   (this is done with the checkactnWlk() function).

  • If layer is visible, run the function selectactnWlk();    (This will basically show the layers I want and hide the layers I don't want and end the loop).
  • If layer is not visible, run the function checkactnRun();      (This will move on to the next layer to check for visibilty).

Select the layer named actnRun          (this is done with the checkactnRun(); function).

  • If layer is visible, run the function selectactnRun();
  • If layer is not visible, run the function checkactnFly();      (This will move on to the next layer to check for visibility and end the loop).

Select the layer named actnFly          (this is done with the checkactnFly(); function).

  • If layer is visible, run the function selectactnFly();
  • If layer is not visible for any of the above layers, then stop the loop and show message saying something like "no layers selected".

Is this possible? Again, any help is highly appreciated.

Chuck Uebele
Community Expert
Community Expert
April 20, 2016

You can pretty much record your actions as scripts by using scriptlistener to record the steps. Once you get the code for all that, the rest is pretty easy also. you just need to select the layers, by whatever means you want then check for the layer being visible:

var doc = activeDocument;

var theFirstLayer //get this however you want

if(theFirstLayer.visible){

//put function for if visible

}

else{

//put function if not visible, etc.

}