skip to Main Content

I’m learning JavaScript for use with photoshop. Im trying to select a layer in a document using a wildcard. I come from a maxscript background so using “*” as a wildcard in strings.

var srcDoc = app.activeDocument;

var a = srcDoc.artLayers.getByName("*t*"); //trying to select a layer that has t in it..

app.activeDocument.activeLayer = a;

3

Answers


  1. Chosen as BEST ANSWER

    Thanks Cobra_Fast

    Its so close its throwing back undefined not an object in the if statement line. But like you said artLayers is acting as an array, so im a bit confused.


  2. According to this bit of documentation, ArtLayers.getByName() only supports searching for exact matches.
    If Photoshop does not provide a searching mechanic, you will have to iterate through all elements in attempt to find matching ones.
    Your code could look something like this:

    var search = "t";
    
    for (var i in srcDoc.artLayers)
    {
        var layer = srcDoc.artLayers[i];
        if (layer.typename.indexOf(search) >= 0)
        {
            // assuming ArtLayer.typename is the correct name property
            // then this layer matches the search!
            // do with it what you want here
        }
    }
    
    Login or Signup to reply.
  3. Welcome to SO. Despite the downvotes I like your question, I’m surprised PS doesn’t have a fuzzy layer name search already.

    I’ll get you up to speed:

    Looping over layers is easy.

    var srcDoc = app.activeDocument;
    var numOfLayers = srcDoc.layers.length;
    
    for (var i = numOfLayers -1; i >= 0  ; i--)
    {
      var thisLayer = srcDoc.layers[i];
    }
    

    However, this is made more complex with layer groups and becomes as right pain at times. So you have to have a recursive function that’ll find them all. It took me a while to get my head around that one! Since we’re only dealing with art layers we’ll add them to a special artlayers array.

    You can then use regular expressions (unavailable in Maxscript:)) to look over an array of layer names.

    // group layer vegetables
    var allLayers = new Array();
    var artLayers = new Array();
    var theLayers = collectAllLayers(app.activeDocument, 0);
    
    
    var artLayerNames = "";
    // loop over art layers backwards
    for (var i = artLayers.length -1; i >= 0  ; i--)
    {
      var temp = artLayers[i];
      var regEx = new RegExp(/t/gim);
      if (temp.match(regEx))
      {
        // if the layer contains the letter "t" show it!
        alert(temp);
      }
      artLayerNames+= artLayers[i] + "n";
    }
    
    // print out all layers
    alert(artLayerNames);
    
    
    
    // function collect all layers
    function collectAllLayers (theParent, level)
    {
      for (var m = theParent.layers.length - 1; m >= 0; m--)
      {
        var theLayer = theParent.layers[m];
        // apply the function to layersets;
        if (theLayer.typename == "ArtLayer")
        {
          // get the art layers
          artLayers.push(theLayer.name);
        }
        else
        {
          allLayers.push(level + theLayer.name);
          collectAllLayers(theLayer, level + 1)
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search