Skip to main content
morrisolmsted
Participant
March 6, 2018
Question

Copying Alpha from one image and applying it to another as mask with Javascript?

  • March 6, 2018
  • 3 replies
  • 833 views

Hey everyone, 

I am working on a tool for my studio that needs to take an alpha from one image and applies it to an image mask on another image.  I have been using script listener but the code that it is spitting out is not too helpful.  Does anyone have experience doing something like this or can point me in the right direction?

Much appreciated,

Morris

This topic has been closed for replies.

3 replies

Legend
March 6, 2018

Try it, but set your parameters )

var dst_doc = app.documents[0]; // define your own destination document

var src_doc = app.documents[1]; // define your own document with alpha channel

var alpha_name = "Alpha 1";     // define your own alpha channel name in src_doc

app.activeDocument = dst_doc;

// create selection in dst_doc based on alpha channel from src_doc

var r = new ActionReference();

r.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );

var d = new ActionDescriptor();

d.putReference( charIDToTypeID( "null" ), r );

var r = new ActionReference();

r.putName( charIDToTypeID( "Chnl" ), alpha_name );

r.putIdentifier( charIDToTypeID( "Dcmn" ), src_doc.id);

d.putReference( charIDToTypeID( "T   " ), r );

executeAction( charIDToTypeID( "setd" ), d, DialogModes.NO );

// create mask from selection

var d = new ActionDescriptor();

d.putClass( charIDToTypeID( "Nw  " ), charIDToTypeID( "Chnl" ));

var r = new ActionReference();

r.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Msk " ) );

d.putReference( charIDToTypeID( "At  " ), r );

d.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "UsrM" ), charIDToTypeID( "RvlS" ) );

executeAction( charIDToTypeID( "Mk  " ), d, DialogModes.NO );

morrisolmsted
Participant
March 6, 2018

at the first executeAction on line 21, this brings up a dialog box...is there a way to not have this happen?

Also the second one at line 34, crashes out with an unknown error...any ideas?

Legend
March 6, 2018

You should have set the first three variables to your values. After all, I do not know how you determine from which document to which you want to copy the alpha channel and what its name (or index, or ID). I do not understand English well and you do not explain your task well (there is not enough input data).

UPD. An error in line 34 can occur if the alpha channel is absolutely black. To avoid this, you need to do additional verification for the presence of the selection.

The simplest (but stupid)

var selection_present = true;

try { activeDocument.selection.bounds } catch (e) { selection_present = false }

if (selection_present) 

    d.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "UsrM" ), charIDToTypeID( "RvlS" ) ); 

else

    d.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "UsrM" ), charIDToTypeID( "HdAl" ) ); 

Also, you need to check for the presence of the old mask ofr the layer and the fact that the layer is selected (active).

UPD2.

Also, if you use Photoshop CS6 then the layer in the final document should not be the Background )

Akash Sharma
Legend
March 6, 2018

Moving to Photoshop Scripting

Kukurykus
Legend
March 6, 2018