1 Reply Latest reply on Jul 1, 2014 9:42 PM by JJMack

    implementing the traverseLayers function from xtools stdlib.js

    ojodegato Level 1

      I am trying to figure how to make a script which can search for a specific layer name in the layer palette, layer group and nested layer groups.

      I came across the xtools traverse layer function in the sdlib.js which might do the job. How to  implement this function was not clear to me.

      I would like to ask help from the scripting community to figure out how apply to this function to a script. I am not a programer and have basic javascript knowledge.

      thanks in advance

        • 1. Re: implementing the traverseLayers function from xtools stdlib.js
          JJMack Most Valuable Participant

          To process all layer you need to use recursion. And layer names in a document need not be unique you can have many layers with the same name.

           

          I played around once with a script to look at document layers to get a little better understand as to how Photoshop works.

           

          // A Photoshop Script by JJMack's

          // This script processes ArtLayers.

          // This script is supplied as is. It is provided as freeware.

          // The author accepts no liability for any problems arising from its use.

          /*

          <javascriptresource>

          <about>$$$/JavaScripts/processArtLayers/About=JJMack's Process All Layers.^r^rCopyright 2009 Mouseprints.^r^rScript utility for action.^rNOTE:Deletes Seleted Area from background and all normal layers.</about>

          </javascriptresource>

          */

          //<category>JJMack's Script</category>

          // enable double-clicking from Mac Finder or Windows Explorer

          #target photoshop // this command only works in Photoshop CS2 and higher

           

          // bring application forward for double-click events

          app.bringToFront();

           

          // ensure at least one document open

          if (!documents.length) {

            alert('There are no documents open.', 'No Document');

          }

           

          // if at least one document exists, then proceed

          else {

            app.activeDocument.suspendHistory('processArtLayers','main()');

          }

           

          ///////////////////////////////////////////////////////////////////////////////

          // main - main function

          ///////////////////////////////////////////////////////////////////////////////

          function main() {

            // declare local variables

            var saveactiveLayer = activeDocument.activeLayer;

            // Set the ruler units to PIXELS

            var orig_ruler_units = app.preferences.rulerUnits;

            app.preferences.rulerUnits = Units.PIXELS;

            try {

            processArtLayers(activeDocument);

            removeAllEmptyLayerSets(activeDocument);

            }

            // display error message if something goes wrong

            catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }

            try { activeDocument.activeLayer = saveactiveLayer;}

            catch(e) { } // may have been deleted

            // Reset units to original settings

            app.preferences.rulerUnits = orig_ruler_units;

          }

          ///////////////////////////////////////////////////////////////////////////////

          // End - main function

          ///////////////////////////////////////////////////////////////////////////////

           

          ///////////////////////////////////////////////////////////////////////////////

          // Function: processsArtLayers

          // Input: document or layer set

          // Return: <none>, all layers that were invisible are now gone

          ///////////////////////////////////////////////////////////////////////////////

          function processArtLayers(obj) {

              for( var i = obj.artLayers.length-1; 0 <= i; i--) {

                  try {

                      processLayer(obj.artLayers[i]);

            }

                  catch (e) {

                  }

              }

              for( var i = obj.layerSets.length-1; 0 <= i; i--) {

            infoLayer(obj.layerSets[i]);    // Display layerSet informarion

                  processArtLayers(obj.layerSets[i]);

              }

          }

           

          ///////////////////////////////////////////////////////////////////////////////

          // Function: removeAllEmptyLayerSets

          // Usage: find all empty layer sets and remove them, recursively

          // Input: document or layer set

          // Return: empty layer sets are now gone

          ///////////////////////////////////////////////////////////////////////////////

          function removeAllEmptyLayerSets(obj) {

              var foundEmpty = true;

              for( var i = obj.layerSets.length-1; 0 <= i; i--) {

                  if( removeAllEmptyLayerSets(obj.layerSets[i])) {

                      obj.layerSets[i].remove();

                  } else {

                      foundEmpty = false;

                  }

              }

              if (obj.artLayers.length > 0) {

            foundEmpty = false;

            }

              return foundEmpty;

          }

           

          function processLayer(layer) {

            infoLayer(layer);

          }

           

          function infoLayer(layer){

            try{

           

            alert("layer = " + layer

            + "\rallLocked = " + layer.allLocked

            + "\rblendMode = " + layer.blendMode

            + "\rbounds = " + layer.bounds

            + "\rfillOpacity = " + layer.fillOpacity

          // + "\rfilterMaskDensity = " + layer.filterMaskDensity // undefined for groups not valid others ??

          // + "\rfilterMaskFather = " + layer.filterMaskFeather // undefined for groups not valid others ??

            + "\rgrouped = " + layer.grouped

            + "\risBackgroundLayer = " + layer.isBackgroundLayer

            + "\rkind = " + layer.kind

            + "\rlayerMaskDensity = " + layer.layerMaskDensity

            + "\rlayerMaskFeather = " + layer.layerMaskFeather

            + "\rlinkedLayers = " + layer.linkedLayers

            + "\rname = " + layer.name

            + "\ropacity = " + layer.opacity

            + "\rparent = " + layer.parent

            + "\rpixelsLocked = " + layer.pixelsLocked

            + "\rpositionLocked = " + layer.positionLocked

          // + "\rtextItem = " + layer.textItem // Valid only when kind = LayerKind.TEXT.

            + "\rtransparentPixelsLocked = " + layer.transparentPixelsLocked

            + "\rtypename = " + layer.typename

            + "\rvectorMaskDensity = " + layer.vectorMaskDensity

            + "\rvectorMaskFeather = " + layer.vectorMaskFeather

            + "\rvisible = " + layer.visible

            + "\rxmpMetadata = " + layer.xmpMetadata

            );

            }

            catch(e){

            }

          };

           

          /*

          allLocked               * boolean read-write. true to completely lock the contents and settings of this layer.

          blendMode               * blendmode read-write. the blending mode.

          bounds                  * array of unitvalue read-only. an array of coordinates that describes the bounding rectangle of the layer.

          fillOpacity             * number [0.0..100] read-write. the interior opacity of the layer, a percentage value.

          filterMaskDensity        double read-write. the density of the filter mask (between 0.0 and 250.0)

          filterMaskFeather        double read-write. the feather of the filter mask (between 0.0 and 250.0)

          grouped                 * boolean read-write. true if this layer is grouped with the layer beneath it.

          isBackgroundLayer       * boolean read-write. true if this is the background layer of the document. a document can have only one background layer. if there is no background layer,

                                   setting this to true causes this to become the background layer.

          kind                    * layerkind read-write. sets the type (such as 'text layer') for an empty layer. valid only when the layer is empty and when isbackgroundlayer.

                                   you can use the kind property to make a background layer a normal layer; however, to make a layer a background layer, you must set isbackgroundlayer to true.

          layerMaskDensity        * double read-write. the density of the layer mask (between 0.0 and 100.0)

          layerMaskFeather        * double read-write. the feather of the layer mask (between 0.0 and 250.0)

          linkedlayers            * array of artlayer or layerset read-only. the layers linked to this layer. see artlayer.link.

          name                    * string read-write. the name.

          opacity                 * number [0.0..100.0]. read-write. the master opacity of the layer, a percentage value.

          parent                  * document read-only. the object's container.

          pixelsLocked            * boolean read-write. true if the pixels in the layer’s image cannot be edited using the paintbrush tool.

          positionLocked          * boolean read-write. true if the pixels in the layer’s image cannot be moved within the layer.

          textitem                 textitem read-only. the text item that is associated with the layer. valid only when kind = layerkind.text.

          transparentpixelslocked * boolean read-write. true if editing is confined to the opaque portions of the layer.

          typename                * string read-only. the class name of the referenced artlayer object.

          vectorMaskDensity       * double read-write. the density of the vector mask (between 0.0 and 250.0)

          vectorMaskFeather       * double read-write. the feather of the vector mask (between 0.0 and 250.0)

          visible                 * boolean read-write. true if the layer is visible.

          xmpMetadata             * xmpmetadata read-write. metadata for the layerb

          */