Skip to main content
Participant
May 20, 2025
Question

Photoshop script running out of sequence

  • May 20, 2025
  • 1 reply
  • 163 views

I'm trying to script a deep etching script for photoshop. I've already got an action for it but I use it on many pictures at once and it's starting to affect my hands as an RSI.
I've coded in an ability to select using the object selection tool and it works really well but when I run doAction("Cut", "My Actions");
It runs doAction first and then runs the selection even though the selection is first in execution order. The action is dependent on the selection. Why is this happening?

1 reply

chamkeyAuthor
Participant
May 20, 2025
function objectSelectionToolFromRect (x1, y1, x2, y2) {
    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 desc14 = new ActionDescriptor();
    var idtop = stringIDToTypeID( "top" );
    var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
    desc14.putUnitDouble( idtop, idpixelsUnit, y1 );
    var idleft = stringIDToTypeID( "left" );
    desc14.putUnitDouble( idleft, idpixelsUnit, x1 );
    var idbottom = stringIDToTypeID( "bottom" );
    desc14.putUnitDouble( idbottom, idpixelsUnit, y2 );
    var idright = stringIDToTypeID( "right" );
    desc14.putUnitDouble( idright, idpixelsUnit, x2 );
    var idrectangle = stringIDToTypeID( "rectangle" );
    desc4.putObject( idto, idrectangle, desc14 );
    var iddeepSelect = stringIDToTypeID( "deepSelect" );
    desc4.putBoolean( iddeepSelect, true );
    var idobjectSelectionMode = stringIDToTypeID( "objectSelectionMode" );
    desc4.putInteger( idobjectSelectionMode, 0 ); // 0 for new selection
    var idmagicLassoAutoEnhance = stringIDToTypeID( "magicLassoAutoEnhance" );
    desc4.putBoolean( idmagicLassoAutoEnhance, true );
    var idsmartSubtract = stringIDToTypeID( "smartSubtract" );
    desc4.putBoolean( idsmartSubtract, true );
    executeAction( idset, desc4, DialogModes.NO );
};
chamkeyAuthor
Participant
May 20, 2025
    for (var i = 0; i < app.documents.length; i++) {
        var doc = app.documents[i];
        app.activeDocument = doc;

        // 3. Flatten Image
        doc.flatten();

        // 4. Increase Canvas Size
        var originalWidth = doc.width;
        var originalHeight = doc.height;
        var newWidth = originalWidth * 1.3;
        var newHeight = originalHeight * 1.3;
        doc.resizeCanvas(newWidth, newHeight, AnchorPosition.MIDDLECENTER);

        // 5. Attempt Automated Selection
        // Use a small rectangle in the top-left corner
        objectSelectionToolFromRect(0, 0, 50, 50); // Coordinates for a 50x50px rectangle
        $.sleep(50); // Add a small delay to allow selection to finalize (milliseconds)

        // 6. Invert Selection
        doc.selection.invert();

        // 7. Run Photoshop Action "cut" from "Sarfaraaz" action folder
        app.doAction("Cut", "My Actions");