Highlighted

Scripting Magic Wand selection by preset color

New Here ,
Oct 19, 2020

Copy link to clipboard

Copied

Hello,

 

I'm trying to write a script to use the magic wand to select a specific color and contract the selection by 3 pixels. However, the magic wand script requires identify the location of a pixel which doesn't work for me because the location of the specific color changes from image to image. Is there any way to get around this?

 

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 );
};

 

Adobe Community Professional
Correct answer by r-bin | Adobe Community Professional
You can add an empty layer,
draw a pixel of the desired color in it at position (0,0),
move the layer to position (-1, -1),
apply magicWand (-1, -1),
delete the temporary layer.
 
TOPICS
Actions and scripting, How to, Windows

Views

68

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Scripting Magic Wand selection by preset color

New Here ,
Oct 19, 2020

Copy link to clipboard

Copied

Hello,

 

I'm trying to write a script to use the magic wand to select a specific color and contract the selection by 3 pixels. However, the magic wand script requires identify the location of a pixel which doesn't work for me because the location of the specific color changes from image to image. Is there any way to get around this?

 

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 );
};

 

Adobe Community Professional
Correct answer by r-bin | Adobe Community Professional
You can add an empty layer,
draw a pixel of the desired color in it at position (0,0),
move the layer to position (-1, -1),
apply magicWand (-1, -1),
delete the temporary layer.
 
TOPICS
Actions and scripting, How to, Windows

Views

69

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 19, 2020 0
Most Valuable Participant ,
Oct 19, 2020

Copy link to clipboard

Copied

If you kown the color use color range instead of the magic wand. get its lab valuse.

function colorRange(fuzziness, luminance, a, b, luminance2, a2, b2, colorModel) {
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var descriptor3 = new ActionDescriptor();
	descriptor.putInteger( stringIDToTypeID( "fuzziness" ), fuzziness );
	descriptor2.putDouble( stringIDToTypeID( "luminance" ), luminance );
	descriptor2.putDouble( stringIDToTypeID( "a" ), a );
	descriptor2.putDouble( stringIDToTypeID( "b" ), b );
	descriptor.putObject( stringIDToTypeID( "minimum" ), stringIDToTypeID( "labColor" ), descriptor2 );
	descriptor3.putDouble( stringIDToTypeID( "luminance" ), luminance2 );
	descriptor3.putDouble( stringIDToTypeID( "a" ), a2 );
	descriptor3.putDouble( stringIDToTypeID( "b" ), b2 );
	descriptor.putObject( charIDToTypeID( "Mxm " ), stringIDToTypeID( "labColor" ), descriptor3 );
	descriptor.putInteger( stringIDToTypeID( "colorModel" ), colorModel );
	executeAction( stringIDToTypeID( "colorRange" ), descriptor, DialogModes.NO );
}
JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2020 2
Adobe Community Professional ,
Oct 19, 2020

Copy link to clipboard

Copied

You can add an empty layer,
draw a pixel of the desired color in it at position (0,0),
move the layer to position (-1, -1),
apply magicWand (-1, -1),
delete the temporary layer.
 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2020 1
New Here ,
Oct 21, 2020

Copy link to clipboard

Copied

I had trouble with finding the right command to add a layer. So I recorded the original color of a pixel in coordinate (0,0), colored pixel in (0,0), and used magic wand on pixel (0,0). When I was done with the pixel, I colored it back to the original color. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 21, 2020 0
Adobe Community Professional ,
Oct 21, 2020

Copy link to clipboard

Copied

activeDocument.artLayers.add()

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 21, 2020 0