Skip to main content
Inspiring
July 8, 2010
Answered

Script with magic wand

  • July 8, 2010
  • 1 reply
  • 4879 views

I noticed there's no documentaion on scripting (javascript) with the magic wand selection. I'd assume that it's along the lines of magicwand.(X,Y).tolerance.contigious.true.samplealllayers.true. or something like that. But it's guess work.

var id176 = charIDToTypeID( "setd" );
    var desc34 = new ActionDescriptor();
    var id177 = charIDToTypeID( "null" );
        var ref15 = new ActionReference();
        var id178 = charIDToTypeID( "Chnl" );
        var id179 = charIDToTypeID( "fsel" );
        ref15.putProperty( id178, id179 );
    desc34.putReference( id177, ref15 );
    var id180 = charIDToTypeID( "T   " );
        var desc35 = new ActionDescriptor();
        var id181 = charIDToTypeID( "Hrzn" );
        var id182 = charIDToTypeID( "#Pxl" );
        desc35.putUnitDouble( id181, id182, 0.000000 );
        var id183 = charIDToTypeID( "Vrtc" );
        var id184 = charIDToTypeID( "#Pxl" );
        desc35.putUnitDouble( id183, id184, 0.000000 );
    var id185 = charIDToTypeID( "Pnt " );
    desc34.putObject( id180, id185, desc35 );
    var id186 = charIDToTypeID( "Tlrn" );
    desc34.putInteger( id186, 32 );
    var id187 = charIDToTypeID( "Mrgd" );
    desc34.putBoolean( id187, true );
    var id188 = charIDToTypeID( "AntA" );
    desc34.putBoolean( id188, true );
executeAction( id176, desc34, DialogModes.NO );

I've got the above by the old script listener and I was just trying to tidy it up (and learn a bit in the process)

Cheers

This topic has been closed for replies.
Correct answer Michael_L_Hale

This is one of the things you can only do with scriptlistener. Here is one way to clean up the code.

function magicWand(x,y,t,a,c,s) {
    if(arguments.length < 2) return;// make sure have x,y
    if(undefined == t) var t = 32;// set defaults of optional arguments
    if(undefined == a) var a = true;
    if(undefined == c) var c = false;
     if(undefined == s) var s = false;
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
    desc.putReference( charIDToTypeID('null'), ref );
        var positionDesc = new ActionDescriptor();
        positionDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), x );// in pixels
        positionDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), y );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Pnt '), positionDesc );
    desc.putInteger( charIDToTypeID('Tlrn'), t);// tolerance
    desc.putBoolean( charIDToTypeID('Mrgd'), s );// sample all layers
    if(!c) desc.putBoolean( charIDToTypeID( "Cntg" ), false );//  contiguous
    desc.putBoolean( charIDToTypeID('AntA'), a );// anti-alias
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};

You could clean it up more by adding data type checks for the arguments.

1 reply

Michael_L_HaleCorrect answer
Inspiring
July 8, 2010

This is one of the things you can only do with scriptlistener. Here is one way to clean up the code.

function magicWand(x,y,t,a,c,s) {
    if(arguments.length < 2) return;// make sure have x,y
    if(undefined == t) var t = 32;// set defaults of optional arguments
    if(undefined == a) var a = true;
    if(undefined == c) var c = false;
     if(undefined == s) var s = false;
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
    desc.putReference( charIDToTypeID('null'), ref );
        var positionDesc = new ActionDescriptor();
        positionDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), x );// in pixels
        positionDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), y );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Pnt '), positionDesc );
    desc.putInteger( charIDToTypeID('Tlrn'), t);// tolerance
    desc.putBoolean( charIDToTypeID('Mrgd'), s );// sample all layers
    if(!c) desc.putBoolean( charIDToTypeID( "Cntg" ), false );//  contiguous
    desc.putBoolean( charIDToTypeID('AntA'), a );// anti-alias
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};

You could clean it up more by adding data type checks for the arguments.

monica_L6890535
Participating Frequently
October 12, 2017

This is a really useful script. Thank you a lot!

Do you know how to inverse the selection?

c.pfaffenbichler
Community Expert
Community Expert
October 13, 2017

Unfortunately Mike Hale is no longer with us.

Inverting a Selection should be do-able by DOM-code:

activeDocument.selection.invert();