Turn layer into selection

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

aram535

Turn layer into selection

Post by aram535 »

Hi,

I need to turn a layer (smaller than canvas) into a selection. Is there a non-recoded method that does this?

I tried:

Code: Select alldoc.layers.getByName("image").selection.selectAll();

but that selects the canvas. I just need the layer's content selected.
Mike Hale

Turn layer into selection

Post by Mike Hale »

You can use Action Manager to load the layer's transparency channel as a selection.
Code: Select allfunction loadTransparency(){
   var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
        var ref1 = new ActionReference();
        ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );
    desc.putReference( charIDToTypeID( "T   " ), ref1 );
   executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
};
aram535

Turn layer into selection

Post by aram535 »

I was so hoping that I wouldn't have to use action manager. It just makes things so much more difficult in case of debugging or future changes.

This works great though, thank you.
Mike Hale

Turn layer into selection

Post by Mike Hale »

For whatever reason Adobe did not add the component or transparency channel to the DOM. If you want to access those channels you have to use Action Manager( which is just a subset of the DOM, ActionDescriptor, etc are DOM classes and executeAction, etc are Application methods ). You do have to understand AM to maintain or debug the code but you can say the same thing about the DOM.

If you want to not use AM and your layer is square/rectangle with the edges parallel to the doc edges you can get the layer's bounds and make your selection from that.
aram535

Turn layer into selection

Post by aram535 »

Yeah makes sense. Is there any guide on the use/function of the AM toolset?
I've been reading the Stdlib functions and the like to get a better understanding of how it works. I've even tried to implement my own sub functions but I always run into a "program error" with no further information as what the error/requirement is that I'm missing.
Mike Hale

Turn layer into selection

Post by Mike Hale »

aram535 wrote:Is there any guide on the use/function of the AM toolset?
No, not really. Xbytor and his xtools is a good source as is here.

Most of the AM code you will see is just 'cleaned up' scriptlistener log entries like the one I posted above.