Skip to main content
Participant
September 29, 2024
Answered

Photoshop Script - trying to set selection to a layer and to align a different layer to it

  • September 29, 2024
  • 2 replies
  • 161 views
       
im trying to align a layer to top and left of another layer, i found a code that does the align but the selection part i changed isn't working, how do i set selection to a specific layer i have?
thanks.
 

 

        Doc.selection.select(ImgLocation);
        Doc.activeLayer = layer;

        var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
        desc.putReference(charIDToTypeID("null"), ref);
        desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID('AdLf'));
        desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID('AdTp'));
        try {
            executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
        } catch (e) {}

        Doc.selection.deselect();

 

This topic has been closed for replies.
Correct answer Stephen Marsh

A couple of ways:

 

1) Using the ScriptingListener plugin to record the Ctrl/Cmd click on the layer icon or the load selection menu command from transparency channel for the layer.

 

2) Get layer bounds coordinates for the layer and use to create a rectangular selection.


P.S. How will you reference the layer? Making it active/selected? By name? By index etc?

 

EDIT: Presuming that the target layer is active you can use the following function call and function to load the layer transparency as a selection:

 

 

layerTransToSel();
function layerTransToSel() {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putProperty( s2t( "channel" ), s2t( "selection" ));
	descriptor.putReference( s2t( "null" ), reference );
	reference2.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "transparencyEnum" ));
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}

 

2 replies

c.pfaffenbichler
Community Expert
Community Expert
September 30, 2024

Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible? 

Can the Layer with the target position be unequivocally be identified by name, position, …? 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
September 29, 2024

A couple of ways:

 

1) Using the ScriptingListener plugin to record the Ctrl/Cmd click on the layer icon or the load selection menu command from transparency channel for the layer.

 

2) Get layer bounds coordinates for the layer and use to create a rectangular selection.


P.S. How will you reference the layer? Making it active/selected? By name? By index etc?

 

EDIT: Presuming that the target layer is active you can use the following function call and function to load the layer transparency as a selection:

 

 

layerTransToSel();
function layerTransToSel() {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putProperty( s2t( "channel" ), s2t( "selection" ));
	descriptor.putReference( s2t( "null" ), reference );
	reference2.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "transparencyEnum" ));
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}