Skip to main content
Desbrina
Inspiring
October 5, 2020
Question

Scripting - Set selection to layer mask

  • October 5, 2020
  • 2 replies
  • 2353 views

I'm trying to set the selected area of a group to the layer mask, then paste and image into it. 

I have it selecting the group and pasting, but not sure how to make it set the selection. At the moment it pastes to the middle of the document.

If i were doing it manually, i'd command+click the layer mask thumbnail

 

    // Layers
    var layers = app.activeDocument.layers;
    var layerName = "Card " + i;
    var myLayer = layers[layerName];
    app.activeDocument.activeLayer = myLayer;
    
    // Selection
    
    
    app.documents[0].paste();

 

I could define the locations manually but then would need to do it for each layer, and there are over 20 layers

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
October 5, 2020

What are you actually trying to automate? 

 

This should load the Layer Mask as a Selection? 

// load layer mask as selection;
// 2020, use it at your own risk;
if (app.documents.length > 0) {main()};
//////
function main () {
// check for layer mask;
var theCheck = hasLayerMask();
if (theCheck == true) {
var idchannel = stringIDToTypeID( "channel" );
var idmask = stringIDToTypeID( "mask" );
// load;
loadSelectionOfLayerMask();
}
};
////// has layer mask //////
function hasLayerMask () {  
    var m_Dsc01, m_Ref01;
    m_Ref01 = new ActionReference();
    m_Ref01.putEnumerated(stringIDToTypeID("layer"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    m_Dsc01 = executeActionGet(m_Ref01);
    return m_Dsc01.hasKey(charIDToTypeID("Usrs"));
    };
////// load layer mask //////
function loadSelectionOfLayerMask() {  
    try {
        var idchannel = stringIDToTypeID( "channel" );
        var desc70 = new ActionDescriptor();
        var ref9 = new ActionReference();
        ref9.putProperty( idchannel, stringIDToTypeID( "selection" ) );
    desc70.putReference( stringIDToTypeID( "null" ), ref9 );
        var ref10 = new ActionReference();
        ref10.putEnumerated( idchannel, idchannel, stringIDToTypeID( "mask" ) );
    desc70.putReference( stringIDToTypeID( "to" ), ref10 );
    executeAction( stringIDToTypeID( "set" ), desc70, DialogModes.NO );
    } catch (_error) {alert (_error)}
    };
Desbrina
DesbrinaAuthor
Inspiring
October 5, 2020

I'm trying to automate copying images from one file into a psd. Currently I copy the image, command+click the layer mask for the first group ( Card 1) then pasting. The copy the second image into Card 2 etc.

 

it's currently copying into the correct groups, but not the correct location

c.pfaffenbichler
Community Expert
Community Expert
October 5, 2020

Have you tried loading the Layer Masks as per above code? 

 

Copy/pasting in a Script is often uselessly wasteful and placing or duplicating might work faster, but frankly I don’t understand your description. What are you really copying – images, layers, …? 

Chuck Uebele
Community Expert
Community Expert
October 5, 2020

What exactly are you making a selection of? Any you want to paste the same selection in mulitple layers? Can you show an example of what you're trying to achieve? Using scripListener would be the best way to handle this, where you could have it generate the code to have a layer mask selected.

 

This code should select the layer mask of the current layer:

var idslct = charIDToTypeID( "slct" );
    var desc36 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref8 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idChnl = charIDToTypeID( "Chnl" );
        var idMsk = charIDToTypeID( "Msk " );
        ref8.putEnumerated( idChnl, idChnl, idMsk );
    desc36.putReference( idnull, ref8 );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc36.putBoolean( idMkVs, false );
executeAction( idslct, desc36, DialogModes.NO );