skip to Main Content

I need a script for photoshop which can create a square or rectangular selection (use rectangular marquee tool) and it will snap to the guides line, no matter the position of the guides, sometimes with 2 guides sometimes I will add 4 guides. Or it’ll be ok if the script make a selection from the guide to the bottom of the photo.

make selection snap to guides

Here are some other options that I’m expecting
make selection 1
make selection 2

2

Answers


  1. First up, should understand that SO is not a code writing service. Also, you should include a minimal, reproducible example of code.

    Ok, on with your question:

    You can get the rectangular coordinates of a selection with selection bounds.
    It’s just a case of adding a horizontal or vertical guide to those bounds:
    The code for the guides was taken from the script listener.

    // Switch off any dialog boxes
    displayDialogs = DialogModes.ERROR; // OFF 
    
    // Top, Left, Bottom, Right
    
    var sb = get_selection_bounds();
    if (sb != undefined)
    {
      add_guide(sb[0], "v");
      add_guide(sb[1], "h");
      add_guide(sb[2], "v");
      add_guide(sb[3], "h");
    }
    
    
    // Set Display Dialogs back to normal
    displayDialogs = DialogModes.ALL; // NORMAL
    
    
    
    function add_guide(dist, dir)
    {
        var vert = true;
        if (dir.toLowerCase() == "v") vert = true;
        if (dir.toLowerCase() == "h") vert = false;
    
        var idMk = charIDToTypeID( "Mk  " );
        var desc1583 = new ActionDescriptor();
        var idNw = charIDToTypeID( "Nw  " );
        var desc1584 = new ActionDescriptor();
        var idPstn = charIDToTypeID( "Pstn" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc1584.putUnitDouble( idPstn, idPxl, dist );
        var idOrnt = charIDToTypeID( "Ornt" );
        var idOrnt = charIDToTypeID( "Ornt" );
        if (vert == true)
        {
            var idVrtc = charIDToTypeID( "Vrtc" );
            desc1584.putEnumerated( idOrnt, idOrnt, idVrtc );
        }
        else
        {
            var idHrzn = charIDToTypeID( "Hrzn" );
            desc1584.putEnumerated( idOrnt, idOrnt, idHrzn );
        }
        var idKnd = charIDToTypeID( "Knd " );
        var idKnd = charIDToTypeID( "Knd " );
        var idDcmn = charIDToTypeID( "Dcmn" );
        desc1584.putEnumerated( idKnd, idKnd, idDcmn );
        var idnull = charIDToTypeID( "null" );
        var ref457 = new ActionReference();
        var idDcmn = charIDToTypeID( "Dcmn" );
        ref457.putIdentifier( idDcmn, 1015 );
        var idGd = charIDToTypeID( "Gd  " );
        ref457.putIndex( idGd, 1 );
        desc1584.putReference( idnull, ref457 );
        var idGd = charIDToTypeID( "Gd  " );
        desc1583.putObject( idNw, idGd, desc1584 );
        var idnull = charIDToTypeID( "null" );
        var ref458 = new ActionReference();
        var idGd = charIDToTypeID( "Gd  " );
        ref458.putClass( idGd );
        desc1583.putReference( idnull, ref458 );
        var idguideTarget = stringIDToTypeID( "guideTarget" );
        var idguideTarget = stringIDToTypeID( "guideTarget" );
        var idguideTargetCanvas = stringIDToTypeID( "guideTargetCanvas" );
        desc1583.putEnumerated( idguideTarget, idguideTarget, idguideTargetCanvas );
        executeAction( idMk, desc1583, DialogModes.NO );
    }
    
    
    
    // function GET SELECTION BOUNDS ()
    // ----------------------------------------------------------------
    function get_selection_bounds()
    {
      try
      {
        var t = parseFloat(app.activeDocument.selection.bounds[0]);
        var l = parseFloat(app.activeDocument.selection.bounds[1]);
        var b = parseFloat(app.activeDocument.selection.bounds[2]);
        var r = parseFloat(app.activeDocument.selection.bounds[3]);
    
        // T, L, B, R
        // return the results as an array
        return [t, l, b, r];
     }
     catch(eek)
     {
       return undefined;
     }
    
    }
    

    selection creates guides

    Login or Signup to reply.
  2. Here script that creates a rectangular selection:

    // Get the active document
    var doc = app.activeDocument;
    
    // Get the guides
    var guides = doc.guides;
    
    // Get the first horizontal and vertical guides
    var hGuide1 = guides[0];
    var vGuide1 = guides[1];
    
    // Get the second horizontal and vertical guides (if they exist)
    var hGuide2 = guides.length > 2 ? guides[2] : null;
    var vGuide2 = guides.length > 3 ? guides[3] : null;
    
    // Calculate the selection coordinates based on the guides
    var x1 = vGuide1.direction == Direction.VERTICAL ? vGuide1.coordinate : hGuide1.coordinate;
    var y1 = hGuide1.direction == Direction.HORIZONTAL ? hGuide1.coordinate : vGuide1.coordinate;
    var x2 = vGuide2 != null && vGuide2.direction == Direction.VERTICAL ? vGuide2.coordinate : doc.width.as("px");
    var y2 = hGuide2 != null && hGuide2.direction == Direction.HORIZONTAL ? hGuide2.coordinate : doc.height.as("px");
    
    // Create the rectangular selection
    doc.selection.select([[x1, y1], [x2, y1], [x2, y2], [x1, y2]], SelectionType.REPLACE, 0, false);
    

    To run it, create a file with the extension jsx. For example: script.jsx

    Then File->Scripts->Browse… select the file script.jsx

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search