Skip to main content
zainulabd786
New Participant
September 24, 2019
Answered

How to use Magic Wand tool in Photoshop scripting?

  • September 24, 2019
  • 1 reply
  • 2774 views

I am trying to remove the image background using Scripting, I am stuck at the first step. I can't find a way to use Magic Wand tool using scripting in Javascript.

This topic has been closed for replies.
Correct answer Chuck Uebele

What you want to do may not be easy; however, using the magic wand tool is. Just use scriptListener to record using the magic wand tool, then just exchange variables for all the values that you normally set like tolerance, and put in the points where you want the magic wand to sample.

 

function quickSel (x, y, tol){
var idsetd = charIDToTypeID( "setd" );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idfsel = charIDToTypeID( "fsel" );
        ref1.putProperty( idChnl, idfsel );
    desc2.putReference( idnull, ref1 );
    var idT = charIDToTypeID( "T   " );
        var desc3 = new ActionDescriptor();
        var idHrzn = charIDToTypeID( "Hrzn" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc3.putUnitDouble( idHrzn, idPxl, x );
        var idVrtc = charIDToTypeID( "Vrtc" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc3.putUnitDouble( idVrtc, idPxl, y);
    var idPnt = charIDToTypeID( "Pnt " );
    desc2.putObject( idT, idPnt, desc3 );
    var idTlrn = charIDToTypeID( "Tlrn" );
    desc2.putInteger( idTlrn, tol);
    var idAntA = charIDToTypeID( "AntA" );
    desc2.putBoolean( idAntA, true );
    var idCntg = charIDToTypeID( "Cntg" );
    desc2.putBoolean( idCntg, true );
executeAction( idsetd, desc2, DialogModes.NO );
};

1 reply

Chuck Uebele
Chuck UebeleCorrect answer
Community Expert
September 24, 2019

What you want to do may not be easy; however, using the magic wand tool is. Just use scriptListener to record using the magic wand tool, then just exchange variables for all the values that you normally set like tolerance, and put in the points where you want the magic wand to sample.

 

function quickSel (x, y, tol){
var idsetd = charIDToTypeID( "setd" );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idfsel = charIDToTypeID( "fsel" );
        ref1.putProperty( idChnl, idfsel );
    desc2.putReference( idnull, ref1 );
    var idT = charIDToTypeID( "T   " );
        var desc3 = new ActionDescriptor();
        var idHrzn = charIDToTypeID( "Hrzn" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc3.putUnitDouble( idHrzn, idPxl, x );
        var idVrtc = charIDToTypeID( "Vrtc" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc3.putUnitDouble( idVrtc, idPxl, y);
    var idPnt = charIDToTypeID( "Pnt " );
    desc2.putObject( idT, idPnt, desc3 );
    var idTlrn = charIDToTypeID( "Tlrn" );
    desc2.putInteger( idTlrn, tol);
    var idAntA = charIDToTypeID( "AntA" );
    desc2.putBoolean( idAntA, true );
    var idCntg = charIDToTypeID( "Cntg" );
    desc2.putBoolean( idCntg, true );
executeAction( idsetd, desc2, DialogModes.NO );
};
zainulabd786
New Participant
September 24, 2019
Thanks, This works