Search code examples
javascriptscriptingphotoshop

Object Selection with Photoshop scripting


I need to be able to auto select humans in a big number of images. I know their coordinates, width and height, but would ideally prefer to have a more humanoid selection, and it seems to work great via normal object selection via the interface. Now I am wondering if I can do it with Photoshop scripting? Any clues appreciated!

To clarify I'm talking about the Object Selection Tool introduced recently here.

Thanks everyone!


Solution

  • I managed to get this working. In case anyone is interested:

    Assuming the coordinates of the bounding box (top, left, bottom, right) are found.

    var idset = stringIDToTypeID( "set" );
    var desc4 = new ActionDescriptor();
    var idnull = stringIDToTypeID( "null" );
    
    var ref3 = new ActionReference();
    var idchannel = stringIDToTypeID( "channel" );
    var idselection = stringIDToTypeID( "selection" );
    ref3.putProperty( idchannel, idselection );
    desc4.putReference( idnull, ref3 );
    
    var idto = stringIDToTypeID( "to" );
        var desc5 = new ActionDescriptor();
        var idtop = stringIDToTypeID( "top" );
        var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
        desc5.putUnitDouble( idtop, idpixelsUnit, top );
        var idleft = stringIDToTypeID( "left" );
        var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
        desc5.putUnitDouble( idleft, idpixelsUnit, left );
        var idbottom = stringIDToTypeID( "bottom" );
        var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
        desc5.putUnitDouble( idbottom, idpixelsUnit, bottom );
        var idright = stringIDToTypeID( "right" );
        var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
        desc5.putUnitDouble( idright, idpixelsUnit, right );
        
    var idrectangle = stringIDToTypeID( "rectangle" );
    desc4.putObject( idto, idrectangle, desc5 );
    var iddeepSelect = stringIDToTypeID( "deepSelect" );
    desc4.putBoolean( iddeepSelect, true );
    var idobjectSelectionMode = stringIDToTypeID( "objectSelectionMode" );
    desc4.putInteger( idobjectSelectionMode, 0 );
    var idmagicLassoAutoEnhance = stringIDToTypeID( "magicLassoAutoEnhance" );
    desc4.putBoolean( idmagicLassoAutoEnhance, true );
    var idsmartSubtract = stringIDToTypeID( "smartSubtract" );
    desc4.putBoolean( idsmartSubtract, true );
    executeAction( idset, desc4, DialogModes.NO );
    

    These codes can be obtained via the ScriptingListener (available here)