Skip navigation
Currently Being Moderated

Looping for a Layer Name ?

Jul 12, 2012 5:43 AM

I want to create a loop that checks if a layer with a set name is within a group, then save the layer group, if not, look outside any and all layer groups for a layer with a set name, if not, ignore and save the selected layer group ?

 
Replies
  • Currently Being Moderated
    Jul 13, 2012 5:43 AM   in reply to StrongBeaver

    then save the layer group

    What is that even supposed to mean?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 13, 2012 7:44 AM   in reply to StrongBeaver

    Photoshop can save documents, but not groups.

    Do you mean »save a new document with just the layer group«?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 15, 2012 1:48 AM   in reply to StrongBeaver

    Maybe this arrangement can help, it uses a function that can be run on a LayerSet or a Document.

    // 2012, use it at your own risk;
    #target photoshop
    var myDocument = app.activeDocument;
    var theLayerGroup = myDocument.activeLayer;
    if (theLayerGroup.typename == "LayerSet") {
    var theName = "name";
    var theLayers = searchForLayers(theName, theLayerGroup, []);
    if (theLayers.length > 0) {alert ("the layerset contains the layer/s")}
    else {
              var theLayers = searchForLayers(theName, myDocument, []);
              switch (theLayers.length) {
                        case 0:
                        alert ("the document contains no layer of that name");
                        break;
                        case 1:
                        alert ("the document contains the layer");
                        break;
                        default:
                        alert ("the document contains more than one layer of that name");
                        break;
                        }
              }
    };
    ////// function collect all layers //////
    function searchForLayers (theName, theParent, allLayers) {
              if (!allLayers) {var allLayers = new Array} 
              else {};
              for (var m = theParent.layers.length - 1; m >= 0;m--) {
                        var theLayer = theParent.layers[m];
                        if (theLayer.typename == "ArtLayer") {
    // check name;
                                  if (theLayer.name == theName) {allLayers.push(theLayer)}
                                  }
    // apply the function to layersets;
                        else {
                                  allLayers = (searchForLayers(theName, theLayer, allLayers))
                                  }
                        };
              return allLayers
              };
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 15, 2012 1:49 AM   in reply to c.pfaffenbichler

    Changed

    if (theLayers.length == 1) {alert ("the layerset contains the layer")}
    

    to

    if (theLayers.length > 0) {alert ("the layerset contains the layer/s")}
    

    to account for cases when the set may contain more than one layer of the name.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 22, 2012 1:19 AM   in reply to StrongBeaver

    What error specifically?

    On which line?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 22, 2012 11:30 PM   in reply to StrongBeaver

    I have no intention of trouble-shooting your lengthy script.

    With the Script I provided earlier a case like you described (template layer at top level) results in the alert »the document contains the layer«, so I see no issue there.

     

    »ReferenceError:findTemplate is not a function« sounds like there might be an issue with the function findTemplate (though I can’t reproduce the issue) – why you use that function at all I don’t know.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 23, 2012 5:36 AM   in reply to StrongBeaver

    The Script I posted was supposed to serve as an example of how the task can be achieved.

    If it is helpful then amending your larger Script is up to you.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 28, 2012 3:10 AM   in reply to StrongBeaver

    The snippet I provided provides alerts for the various situations, that is where you could insert the corresponding procedures you want to trigger.

    And the array theLayers lists the respectively corresponding template-Layer/s, so that can be used to address them or their parents.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 1, 2012 3:40 AM   in reply to StrongBeaver

    I’m busy currently but I will try to give a more thorough explanation later.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 2:21 AM   in reply to StrongBeaver

    In the code I posted the alerts mark the various places you could use as jumping off points for the different operations you want to perform depending on the situation (template-layer in the layerSet or not etc.).

     

    If you wrap your operation(s) in function(s) you can insert those instead of the alerts.

    If the function takes the template layer and the selected layerSet as arguments you can feed them into the functions there.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 1:02 AM   in reply to StrongBeaver

    This would be an example of how to use a function to jump off from the various points – don’t consider this as a final Script but as an illustration of one possible way.

    I frankly did not see what the dialog was for as the input did not seem to be used later on so I dropped it.

     

    // 2012, use it at your own risk;
    #target photoshop
    if (app.documents.length > 0) {
    ////////////////////////////////////
    // get document-path and -title;
    // 2012, use it at your own risk;
    #target photoshop
    var myDocument = app.activeDocument;
    var theLayerGroup = myDocument.activeLayer;
    if (theLayerGroup.typename == "LayerSet") {
    var theName = "template";
    var theLayers = searchForLayers(theName, theLayerGroup, []);
    if (theLayers.length == 1) {
              doStuff (myDocument, theLayerGroup, undefined);
              }
    else {
              var theLayers = searchForLayers(theName, myDocument, []);
              switch (theLayers.length) {
                        case 0:
                        alert ("the document contains no layer of that name");
                        doStuff (myDocument, theLayerGroup, undefined);
                        break;
                        case 1:
                        doStuff (myDocument, theLayerGroup, theLayers[0]);
                        break;
                        default:
                        alert ("the document contains more than one layer of that name");
                        break;
                        }
              }
    };
    };
    else {alert ("no document open")};
    ////////////////////////////////////
    ////// function collect all layers //////
    function searchForLayers (theName, theParent, allLayers) {
              if (!allLayers) {var allLayers = new Array}
              else {};
              for (var m = theParent.layers.length - 1; m >= 0;m--) {
                        var theLayer = theParent.layers[m];
                        if (theLayer.typename == "ArtLayer") {
    // check name;
                                  if (theLayer.name == theName) {allLayers.push(theLayer)}
                                  }
    // apply the function to layersets;
                        else {
                                  allLayers = (searchForLayers(theName, theLayer, allLayers))
                                  }
                        };
              return allLayers
              };
    ////// delete hidden layers //////
    function deleteHiddenLayers () {
    var id26 = charIDToTypeID( "Dlt " );
    var desc6 = new ActionDescriptor();
    var id27 = charIDToTypeID( "null" );
    var ref6 = new ActionReference();
    var id28 = charIDToTypeID( "Lyr " );
    var id29 = charIDToTypeID( "Ordn" );
    var id30 = stringIDToTypeID( "hidden" );
    ref6.putEnumerated( id28, id29, id30 );
    desc6.putReference( id27, ref6 );
    executeAction( id26, desc6, DialogModes.NO );
    };
    ////// toggle visibility of others //////
    function hideOthers (theLayer, myDocument) {
    myDocument.activeLayer = theLayer;
    // =======================================================
    var idShw = charIDToTypeID( "Shw " );
        var desc10 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var list4 = new ActionList();
                var ref7 = new ActionReference();
                var idLyr = charIDToTypeID( "Lyr " );
                var idOrdn = charIDToTypeID( "Ordn" );
                var idTrgt = charIDToTypeID( "Trgt" );
                ref7.putEnumerated( idLyr, idOrdn, idTrgt );
            list4.putReference( ref7 );
        desc10.putList( idnull, list4 );
        var idTglO = charIDToTypeID( "TglO" );
        desc10.putBoolean( idTglO, true );
    executeAction( idShw, desc10, DialogModes.NO );
    };
    ////// by paul mr //////
    function GetSelectedLayers() {
    var A=[];
        var desc11 = new ActionDescriptor();
            var ref9 = new ActionReference();
            ref9.putClass( stringIDToTypeID('layerSection') );
        desc11.putReference( charIDToTypeID('null'), ref9 );
            var ref10 = new ActionReference();
            ref10.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
        desc11.putReference( charIDToTypeID('From'), ref10 );
        executeAction( charIDToTypeID('Mk  '), desc11, DialogModes.NO );
    var gL = activeDocument.activeLayer.layers;
    for(var i=0;i<gL.length;i++){
    A.push(gL[i]);
    }
    executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
    return A;
    };
    ////// function to find layers of a name //////
    function findTemplate (theParent, theTemplateName) {
    if (!allLayers) {var allLayers = new Array}
    else {};
    for (var m = theParent.layers.length - 1; m >= 0;m--) {
    var theLayer = theParent.layers[m];
    // apply the function to layersets;
    if (theLayer.typename == "ArtLayer") {
         if (theLayer.name == theTemplateName) {
              allLayers.push(theLayer)
              }
         }
    else {
         allLayers = allLayers.concat(findTemplate(theLayer, theTemplateName))
         }
    };
    return allLayers
    };
    ////// the operation //////
    function doStuff (myDocument, thisSet, theTemplate) {
    var myPath = myDocument.path;
    var myDocName = myDocument.name;
    // psd options;
    psdOpts = new PhotoshopSaveOptions();
    psdOpts.embedColorProfile = true;
    psdOpts.alphaChannels = false;
    psdOpts.layers = true;
    psdOpts.spotColors = true;
    ////////////////////////////////////
    // create a history state to return to later;
    myDocument.quickmaskMode = true;
    myDocument.quickmaskMode = false;
    var historyState = myDocument.activeHistoryState;
    // hide other layers;
    hideOthers (thisSet, myDocument);
    var aLayerName = thisSet.name;
    // remove »/« and »:«;
    while (aLayerName.indexOf("/") != -1) {aLayerName = aLayerName.replace("/", "_")};
    while (aLayerName.indexOf(":") != -1) {aLayerName = aLayerName.replace(":", "_")};
    // show template;
    try {
              theTemplate.move(myDocument, ElementPlacement.PLACEATBEGINNING);
              theTemplate.visible = true;
              }
    catch (e) {};
    // duplicate;
    var theCopy = myDocument.duplicate ("thecopy", false);
    deleteHiddenLayers ();
    ///////////////////////
    /// save and close;
    //////////////////////
    theCopy.saveAs((new File(myPath+"/"+myDocName+"_"+aLayerName+".psd")), psdOpts, false);
    theCopy.close(SaveOptions.DONOTSAVECHANGES)
    // return in history;
    myDocument.activeHistoryState = historyState;
    };
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 2:11 AM   in reply to StrongBeaver

    Currently the function uses a try-clause to move a template outside of the selected LayerSet to the top of the file and make it visible.

    You can change the line

              theTemplate.move(myDocument, ElementPlacement.PLACEATBEGINNING); 

    to instead move it to the end of the LayerSet instead.

     

    And if you want to treat template Layers differently according to whether they are top level or inside another LayerSet you could add an if-clause that checks for the template Layer’s parent.

    if (theTemplate.parent == myDocument) {/*do one thing*/}
    else {/*do another*/}
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 12, 2012 12:59 AM   in reply to StrongBeaver

    You should be able to figure it out.

    layerMove.jpg

    »Photoshop JavaScript Ref.pdf« lists the ElementPlacement values.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 12, 2012 10:51 PM   in reply to StrongBeaver

    f the template layer is outside of the layer group I can have it "pulled" into the layerGroup upon exporting the specific layerGroup or not, with a dialog ?

    Yes, a confirm-dialog should suffice for the task.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 13, 2012 4:57 AM   in reply to StrongBeaver

    So what good is a dialog?

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 13, 2012 6:58 AM   in reply to StrongBeaver

    If you always want the template in back then just move it into the back – why complicate the operation?

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points