getByName option not working if layer is inside group

Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Hello Everyone
I have one script which move and place currently selected layer above the "LOGO" layer (LOGO is layer's name). Script is working perfectly when logo layer is not inside any group but if logo layer is inside any group this script doesn't work and give error
Error1302:NO such element 
Line:6
-> var layerRef=app.activeDocument.layers.getByName('LOGO');
What changes are required in this script ? Kindly help. I am using photoshop cs3
Below is my current script

var doc=app.activeDocument;

var moveLayer = doc.activeLayer;  

var layerRef = app.activeDocument.layers.getByName( 'LOGO' );

var targetLayer = layerRef;

moveLayer.move(targetLayer,ElementPlacement.PLACEBEFORE);  



TOPICS
Actions and scripting , Windows

Views

171

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jun 28, 2022 Jun 28, 2022

Try this. You need to start with the layer you want to move selected.

#target photoshop
var doc = activeDocument;
var moveLay = doc.activeLayer
var curLayer
var targetLay
goThroughLayers (doc);

doc.activeLayer = moveLay;
moveLay.move (targetLay, ElementPlacement.PLACEBEFORE)

function goThroughLayers(parentLayer){
    for(var i=0;i<parentLayer.layers.length;i++){
        curLayer = parentLayer.layers[i];
        doc.activeLayer = curLayer;
        if(curLayer.typename =='LayerSet'){goThroughLay
...

Likes

Translate

Translate
Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Update : Currently selected layer could be anything like text layer, smart object layer etc

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

You have to search within the group. Using AM code. To search the entire document, I generally us a recursive function that will go through all layers, and if it comes across a group, it will restart the search within that group. There are faster ways, by using AM code, but I would have to look that up, as I'm not that proficient with it.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Thank you @Chuck Uebele sir. Let me try few more time according to your suggestion.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Hello sir I found one script which select the LOGO layer no matter it is inside group or outside group but I don't know how to make it layer Reference
I mean this ///var layerRef = app.activeDocument.layers.getByName( 'LOGO' );)

here is that script

function makeLayerActiveByName(nm) {
    function cTID(s) { return app.charIDToTypeID(s); };
 
    try {
 
      var desc5 = new ActionDescriptor();
          var ref4 = new ActionReference();
          ref4.putName( cTID('Lyr '),  nm);
      desc5.putReference( cTID('null'), ref4 );
      desc5.putBoolean( cTID('MkVs'), false );
      executeAction( cTID('slct'), desc5, DialogModes.NO );
   
 
      return true;
 
   } catch (e) {
 

     return false;
 
   }
  };
 

 
   makeLayerActiveByName("LOGO");

   

(Sorry if I am trying to solve the problem in a wrong way)


Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

This link, might have a start on using AM code to search all layers. 

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/find-and-select-layer-by-an-specific-...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Thank you sir..Let me check the link and try.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Here's a link with code that I posted for a recursive function. 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-iterating-through-al...

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Sir I checked the code which you mentioned. I am trying to learn. This is what I found

var doc = activeDocument;
var curLayer
goThroughLayers (doc);

function goThroughLayers(parentLayer){
    for(var i=0;i<parentLayer.layers.length;i++){
        curLayer = parentLayer.layers[i];
        doc.activeLayer = curLayer;
        if(curLayer.typename =='LayerSet'){goThroughLayers (curLayer)}
        else{
            if(curLayer.name=='LOGO'){
                if(curLayer.name.match (/[e]/ig)){alert('match')
           
           

//sorry but My question is how to place previously selected above matched layer if match found. I mean this step >> moveLayer.move(targetLayer,ElementPlacement.PLACEBEFORE);  
//How to make the matched layer Reference layer in this link or my posted code >>>> var layerRef = app.activeDocument.layers.getByName( 'LOGO' );

           
           
           
            }
                }//end if
            }//end else
        }//end loop
    }//end function

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

The script mentioned in your link is searching the LOGO layer perfectly  but I am not able to place previously selected layer above logo layer. Please check the screenshot I uploaded with my main post.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

I will have to check later, as I'maway from my computer.. There was an issue with one of the place locations that didn't work with a group. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

This AM based function will select the first layer found by name, even if it is inside nested layer sets of the same name (it only selected layers, not groups/sets).

 

// Select layer by name, not set/group of same name
selectLayerName("LOGO");

function selectLayerName(lyrName) {
var idselect = stringIDToTypeID( "select" );
    var desc266 = new ActionDescriptor();
    var idnull = stringIDToTypeID( "null" );
        var ref59 = new ActionReference();
        var idlayer = stringIDToTypeID( "layer" );
        ref59.putName( idlayer, lyrName );
    desc266.putReference( idnull, ref59 );
    var idmakeVisible = stringIDToTypeID( "makeVisible" );
    desc266.putBoolean( idmakeVisible, false );
    var idlayerID = stringIDToTypeID( "layerID" );
        var list41 = new ActionList();
        list41.putInteger( 3 );
    desc266.putList( idlayerID, list41 );
executeAction( idselect, desc266, DialogModes.NO );
}

 

And here it is "beautified" through Clean SL:

// Select layer by name, not set/group of same name
selectLayerName("LOGO");

function selectLayerName(lyrName) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var list = new ActionList();
	var reference = new ActionReference();
	reference.putName( s2t( "layer" ), lyrName );
	descriptor.putReference( s2t( "null" ), reference );
	descriptor.putBoolean( s2t( "makeVisible" ), false );
	list.putInteger( 3 );
	descriptor.putList( s2t( "layerID" ), list );
	executeAction( s2t( "select" ), descriptor, DialogModes.NO );
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Hello sir, Thanks for the reply but your code is selecting  first layer found by name, even if it is inside nested layer sets of the same name (it only selected layers, not groups/sets).
Selecting LOGO layer is not an issue. I think I am not able to convey my message properly. Please check my original post. I want to move active layer above LOGO layer. I dont want to select LOGO layer. Please check the screenshot attached. The script which is posted in main post move the activelayer above LOGO layer if LOGO layer is not inside any group. Script doesnt work if LOGO layer is inside any group. In my original LOGO layer is used as layer referenece in line 6 

var layerRef = app.activeDocument.layers.getByName( 'LOGO' ); 
I want to know how to make LOGO layer reference layer if it is inside group. Please check the screenshot attacted in my post.

)
Thanks & Regards 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied


@Amelia24456658fkpp wrote:

Selecting LOGO layer is not an issue. I think I am not able to convey my message properly.


 

I thought it was as you originally wrote:

 

Script is working perfectly when logo layer is not inside any group but if logo layer is inside any group this script doesn't work and give error...

 

Apologies, there are probably better ways, however, by selecting the LOGO layer, you can get it's layer ID, then you can move your target layer to the layer ID position above the LOGO layer... Or so I was thinking, I haven't had time to test that though.

 

Looks like Chuck has come to the rescue again!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

This is what I was referring to above. Have the layer active that you would like to move above the layer named LOGO:

 

#target photoshop

var targetLayer = activeDocument.activeLayer;
selectLayerName("LOGO");
var logoID = activeDocument.activeLayer.id;
activeDocument.activeLayer = targetLayer;
moveLayerInStack(logoID + 1);

function selectLayerName(lyrName) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var list = new ActionList();
	var reference = new ActionReference();
	reference.putName( s2t( "layer" ), lyrName );
	descriptor.putReference( s2t( "null" ), reference );
	descriptor.putBoolean( s2t( "makeVisible" ), false );
	list.putInteger( 3 );
	descriptor.putList( s2t( "layerID" ), list );
	executeAction( s2t( "select" ), descriptor, DialogModes.NO );
}

function moveLayerInStack(lyrID) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var list = new ActionList();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "null" ), reference );
	reference2.putIndex( s2t( "layer" ), 4 );
	descriptor.putReference( s2t( "to" ), reference2 );
	descriptor.putBoolean( s2t( "adjustment" ), false );
	descriptor.putInteger( s2t( "version" ), lyrID );
	list.putInteger( 2 );
	descriptor.putList( s2t( "layerID" ), list );
	executeAction( s2t( "move" ), descriptor, DialogModes.NO );
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Thanks sir but I am using photoshop cs3 and this script is giving  error 1243: Illegal argument -argument 2 -numeric value expected , line 37> descriptor.putInteger(s2t("version),lyrID. This might be working fine in new photoshop versions.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 30, 2022 Jun 30, 2022

Copy link to clipboard

Copied

It appears that CS3 doesn't like swapping out the layer ID integer value for a variable. Yes, it does work in newer versions.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

Try this. You need to start with the layer you want to move selected.

#target photoshop
var doc = activeDocument;
var moveLay = doc.activeLayer
var curLayer
var targetLay
goThroughLayers (doc);

doc.activeLayer = moveLay;
moveLay.move (targetLay, ElementPlacement.PLACEBEFORE)

function goThroughLayers(parentLayer){
    for(var i=0;i<parentLayer.layers.length;i++){
        curLayer = parentLayer.layers[i];
        doc.activeLayer = curLayer;
        if(curLayer.typename =='LayerSet'){goThroughLayers (curLayer)}
        else{
            if(curLayer.name=='LOGO'){
                targetLay = curLayer}
            }//end else
        }//end for loop
    }//end function

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Thank you so much sir. Its working perfectly according to my requirements. Thanks for the big help ❤️

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 17, 2022 Aug 17, 2022

Copy link to clipboard

Copied

LATEST

https://youtu.be/I7IW2YYWBLs

Hello sir playase help me

I want to sort the layers

William 1 William 2 William 3 William 4

Your script does the job but it does it on one layer

I want to bring all these layers together one after the other

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines