Skip to main content
mauricior6328708
Inspiring
January 31, 2019
Answered

Create Grid Rectangles inside selected area ("BUG")

  • January 31, 2019
  • 2 replies
  • 3801 views

Hello!

I found in this old post a very useful script written and updated by the brilliant Chuck Uebele

Re: Create Grid Rectangles inside selected area

I decided to create a simple user interface, with row controllers, columns and the size of the space between rows and columns, only today that I noticed an unpleasant failure when the number of rows is different from the amount of columns.

Chuck Uebele, could you check out this video?

Could it be possible to circumvent this failure?

Here is the project as it was last updated:

https://pastebin.com/raw/xQ6ewGnn

Any help is valid! Thank you

This topic has been closed for replies.
Correct answer Chuck Uebele

In line 16, the original script had row as the last variable. It needs to be column. Same with line 17, but it should be row. The below script works.

#target photoshop 

 

var row = 2;   

var column = 3;   

var lineSize = 15  //new

app.preferences.rulerUnits = Units.PIXELS; //new

 

if(app.documents.length>0){ 

    var doc = activeDocument; 

    try{ 

        var sBounds = doc.selection.bounds; 

        var lineAdjW = lineSize/row

        var lineAdjH = lineSize/column

         

        var sWidth = (parseFloat(sBounds[2]) -parseFloat(sBounds[0]))/column - lineSize + lineSize/(column );  //modified

        var sHeight = (parseFloat(sBounds[3]) -parseFloat(sBounds[1]))/row - lineSize + lineSize/(row ) ;  //modified

       

        var leftSide = parseFloat (sBounds[0]); 

        var topSide = parseFloat (sBounds[1]); 

      

        for(var i=0;i<row;i++){ 

            leftSide = parseFloat (sBounds[0]); 

            for(var j=0;j<column;j++){ 

                Create_Rectangle (topSide, leftSide, topSide + sHeight, leftSide + sWidth)  //old code

                leftSide += sWidth + lineSize

                } 

            topSide += sHeight  +lineSize

            } 

         

        }//end try  

    catch(e){alert('There is no selection')} 

    } 

else{alert('There are no open documents')} 

 

 

 

 

function Create_Rectangle(top,left,bottom,right) {   

var idMk = charIDToTypeID( "Mk  " ); 

    var desc14 = new ActionDescriptor(); 

    var idnull = charIDToTypeID( "null" ); 

        var ref4 = new ActionReference(); 

        var idcontentLayer = stringIDToTypeID( "contentLayer" ); 

        ref4.putClass( idcontentLayer ); 

    desc14.putReference( idnull, ref4 ); 

    var idUsng = charIDToTypeID( "Usng" ); 

        var desc15 = new ActionDescriptor(); 

        var idType = charIDToTypeID( "Type" ); 

            var desc16 = new ActionDescriptor(); 

            var idClr = charIDToTypeID( "Clr " ); 

                var desc17 = new ActionDescriptor(); 

                var idRd = charIDToTypeID( "Rd  " ); 

                desc17.putDouble( idRd, 0.000000 ); 

                var idGrn = charIDToTypeID( "Grn " ); 

                desc17.putDouble( idGrn, 0.000000 ); 

                var idBl = charIDToTypeID( "Bl  " ); 

                desc17.putDouble( idBl, 0.000000 ); 

            var idRGBC = charIDToTypeID( "RGBC" ); 

            desc16.putObject( idClr, idRGBC, desc17 ); 

        var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" ); 

        desc15.putObject( idType, idsolidColorLayer, desc16 ); 

        var idShp = charIDToTypeID( "Shp " ); 

            var desc18 = new ActionDescriptor(); 

            var idunitValueQuadVersion = stringIDToTypeID( "unitValueQuadVersion" ); 

            desc18.putInteger( idunitValueQuadVersion, 1 ); 

            var idTop = charIDToTypeID( "Top " ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idTop, idPxl, top ); 

            var idLeft = charIDToTypeID( "Left" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idLeft, idPxl, left ); 

            var idBtom = charIDToTypeID( "Btom" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idBtom, idPxl, bottom ); 

            var idRght = charIDToTypeID( "Rght" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idRght, idPxl, right ); 

            var idtopRight = stringIDToTypeID( "topRight" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idtopRight, idPxl, 0.000000 ); 

            var idtopLeft = stringIDToTypeID( "topLeft" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idtopLeft, idPxl, 0.000000 ); 

            var idbottomLeft = stringIDToTypeID( "bottomLeft" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idbottomLeft, idPxl, 0.000000 ); 

            var idbottomRight = stringIDToTypeID( "bottomRight" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idbottomRight, idPxl, 0.000000 ); 

        var idRctn = charIDToTypeID( "Rctn" ); 

        desc15.putObject( idShp, idRctn, desc18 ); 

        var idstrokeStyle = stringIDToTypeID( "strokeStyle" ); 

            var desc19 = new ActionDescriptor(); 

            var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" ); 

            desc19.putInteger( idstrokeStyleVersion, 2 ); 

            var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" ); 

            desc19.putBoolean( idstrokeEnabled, true ); 

            var idfillEnabled = stringIDToTypeID( "fillEnabled" ); 

            desc19.putBoolean( idfillEnabled, true ); 

            var idstrokeStyleLineWidth = stringIDToTypeID( "strokeStyleLineWidth" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc19.putUnitDouble( idstrokeStyleLineWidth, idPxl, 1.000000 ); 

            var idstrokeStyleLineDashOffset = stringIDToTypeID( "strokeStyleLineDashOffset" ); 

            var idPnt = charIDToTypeID( "#Pnt" ); 

            desc19.putUnitDouble( idstrokeStyleLineDashOffset, idPnt, 0.000000 ); 

            var idstrokeStyleMiterLimit = stringIDToTypeID( "strokeStyleMiterLimit" ); 

            desc19.putDouble( idstrokeStyleMiterLimit, 100.000000 ); 

            var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" ); 

            var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" ); 

            var idstrokeStyleButtCap = stringIDToTypeID( "strokeStyleButtCap" ); 

            desc19.putEnumerated( idstrokeStyleLineCapType, idstrokeStyleLineCapType, idstrokeStyleButtCap ); 

            var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" ); 

            var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" ); 

            var idstrokeStyleMiterJoin = stringIDToTypeID( "strokeStyleMiterJoin" ); 

            desc19.putEnumerated( idstrokeStyleLineJoinType, idstrokeStyleLineJoinType, idstrokeStyleMiterJoin ); 

            var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" ); 

            var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" ); 

            var idstrokeStyleAlignInside = stringIDToTypeID( "strokeStyleAlignInside" ); 

            desc19.putEnumerated( idstrokeStyleLineAlignment, idstrokeStyleLineAlignment, idstrokeStyleAlignInside ); 

            var idstrokeStyleScaleLock = stringIDToTypeID( "strokeStyleScaleLock" ); 

            desc19.putBoolean( idstrokeStyleScaleLock, false ); 

            var idstrokeStyleStrokeAdjust = stringIDToTypeID( "strokeStyleStrokeAdjust" ); 

            desc19.putBoolean( idstrokeStyleStrokeAdjust, false ); 

            var idstrokeStyleLineDashSet = stringIDToTypeID( "strokeStyleLineDashSet" ); 

                var list2 = new ActionList(); 

            desc19.putList( idstrokeStyleLineDashSet, list2 ); 

            var idstrokeStyleBlendMode = stringIDToTypeID( "strokeStyleBlendMode" ); 

            var idBlnM = charIDToTypeID( "BlnM" ); 

            var idNrml = charIDToTypeID( "Nrml" ); 

            desc19.putEnumerated( idstrokeStyleBlendMode, idBlnM, idNrml ); 

            var idstrokeStyleOpacity = stringIDToTypeID( "strokeStyleOpacity" ); 

            var idPrc = charIDToTypeID( "#Prc" ); 

            desc19.putUnitDouble( idstrokeStyleOpacity, idPrc, 100.000000 ); 

            var idstrokeStyleContent = stringIDToTypeID( "strokeStyleContent" ); 

                var desc20 = new ActionDescriptor(); 

                var idClr = charIDToTypeID( "Clr " ); 

                    var desc21 = new ActionDescriptor(); 

                    var idRd = charIDToTypeID( "Rd  " ); 

                    desc21.putDouble( idRd, 121.991998 ); 

                    var idGrn = charIDToTypeID( "Grn " ); 

                    desc21.putDouble( idGrn, 121.991998 ); 

                    var idBl = charIDToTypeID( "Bl  " ); 

                    desc21.putDouble( idBl, 121.991998 ); 

                var idRGBC = charIDToTypeID( "RGBC" ); 

                desc20.putObject( idClr, idRGBC, desc21 ); 

            var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" ); 

            desc19.putObject( idstrokeStyleContent, idsolidColorLayer, desc20 ); 

            var idstrokeStyleResolution = stringIDToTypeID( "strokeStyleResolution" ); 

            desc19.putDouble( idstrokeStyleResolution, 300.000000 ); 

        var idstrokeStyle = stringIDToTypeID( "strokeStyle" ); 

        desc15.putObject( idstrokeStyle, idstrokeStyle, desc19 ); 

    var idcontentLayer = stringIDToTypeID( "contentLayer" ); 

    desc14.putObject( idUsng, idcontentLayer, desc15 ); 

    var idLyrI = charIDToTypeID( "LyrI" ); 

    desc14.putInteger( idLyrI, 27 ); 

executeAction( idMk, desc14, DialogModes.NO ); 

    };   

2 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
January 31, 2019

In line 16, the original script had row as the last variable. It needs to be column. Same with line 17, but it should be row. The below script works.

#target photoshop 

 

var row = 2;   

var column = 3;   

var lineSize = 15  //new

app.preferences.rulerUnits = Units.PIXELS; //new

 

if(app.documents.length>0){ 

    var doc = activeDocument; 

    try{ 

        var sBounds = doc.selection.bounds; 

        var lineAdjW = lineSize/row

        var lineAdjH = lineSize/column

         

        var sWidth = (parseFloat(sBounds[2]) -parseFloat(sBounds[0]))/column - lineSize + lineSize/(column );  //modified

        var sHeight = (parseFloat(sBounds[3]) -parseFloat(sBounds[1]))/row - lineSize + lineSize/(row ) ;  //modified

       

        var leftSide = parseFloat (sBounds[0]); 

        var topSide = parseFloat (sBounds[1]); 

      

        for(var i=0;i<row;i++){ 

            leftSide = parseFloat (sBounds[0]); 

            for(var j=0;j<column;j++){ 

                Create_Rectangle (topSide, leftSide, topSide + sHeight, leftSide + sWidth)  //old code

                leftSide += sWidth + lineSize

                } 

            topSide += sHeight  +lineSize

            } 

         

        }//end try  

    catch(e){alert('There is no selection')} 

    } 

else{alert('There are no open documents')} 

 

 

 

 

function Create_Rectangle(top,left,bottom,right) {   

var idMk = charIDToTypeID( "Mk  " ); 

    var desc14 = new ActionDescriptor(); 

    var idnull = charIDToTypeID( "null" ); 

        var ref4 = new ActionReference(); 

        var idcontentLayer = stringIDToTypeID( "contentLayer" ); 

        ref4.putClass( idcontentLayer ); 

    desc14.putReference( idnull, ref4 ); 

    var idUsng = charIDToTypeID( "Usng" ); 

        var desc15 = new ActionDescriptor(); 

        var idType = charIDToTypeID( "Type" ); 

            var desc16 = new ActionDescriptor(); 

            var idClr = charIDToTypeID( "Clr " ); 

                var desc17 = new ActionDescriptor(); 

                var idRd = charIDToTypeID( "Rd  " ); 

                desc17.putDouble( idRd, 0.000000 ); 

                var idGrn = charIDToTypeID( "Grn " ); 

                desc17.putDouble( idGrn, 0.000000 ); 

                var idBl = charIDToTypeID( "Bl  " ); 

                desc17.putDouble( idBl, 0.000000 ); 

            var idRGBC = charIDToTypeID( "RGBC" ); 

            desc16.putObject( idClr, idRGBC, desc17 ); 

        var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" ); 

        desc15.putObject( idType, idsolidColorLayer, desc16 ); 

        var idShp = charIDToTypeID( "Shp " ); 

            var desc18 = new ActionDescriptor(); 

            var idunitValueQuadVersion = stringIDToTypeID( "unitValueQuadVersion" ); 

            desc18.putInteger( idunitValueQuadVersion, 1 ); 

            var idTop = charIDToTypeID( "Top " ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idTop, idPxl, top ); 

            var idLeft = charIDToTypeID( "Left" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idLeft, idPxl, left ); 

            var idBtom = charIDToTypeID( "Btom" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idBtom, idPxl, bottom ); 

            var idRght = charIDToTypeID( "Rght" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idRght, idPxl, right ); 

            var idtopRight = stringIDToTypeID( "topRight" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idtopRight, idPxl, 0.000000 ); 

            var idtopLeft = stringIDToTypeID( "topLeft" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idtopLeft, idPxl, 0.000000 ); 

            var idbottomLeft = stringIDToTypeID( "bottomLeft" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idbottomLeft, idPxl, 0.000000 ); 

            var idbottomRight = stringIDToTypeID( "bottomRight" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc18.putUnitDouble( idbottomRight, idPxl, 0.000000 ); 

        var idRctn = charIDToTypeID( "Rctn" ); 

        desc15.putObject( idShp, idRctn, desc18 ); 

        var idstrokeStyle = stringIDToTypeID( "strokeStyle" ); 

            var desc19 = new ActionDescriptor(); 

            var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" ); 

            desc19.putInteger( idstrokeStyleVersion, 2 ); 

            var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" ); 

            desc19.putBoolean( idstrokeEnabled, true ); 

            var idfillEnabled = stringIDToTypeID( "fillEnabled" ); 

            desc19.putBoolean( idfillEnabled, true ); 

            var idstrokeStyleLineWidth = stringIDToTypeID( "strokeStyleLineWidth" ); 

            var idPxl = charIDToTypeID( "#Pxl" ); 

            desc19.putUnitDouble( idstrokeStyleLineWidth, idPxl, 1.000000 ); 

            var idstrokeStyleLineDashOffset = stringIDToTypeID( "strokeStyleLineDashOffset" ); 

            var idPnt = charIDToTypeID( "#Pnt" ); 

            desc19.putUnitDouble( idstrokeStyleLineDashOffset, idPnt, 0.000000 ); 

            var idstrokeStyleMiterLimit = stringIDToTypeID( "strokeStyleMiterLimit" ); 

            desc19.putDouble( idstrokeStyleMiterLimit, 100.000000 ); 

            var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" ); 

            var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" ); 

            var idstrokeStyleButtCap = stringIDToTypeID( "strokeStyleButtCap" ); 

            desc19.putEnumerated( idstrokeStyleLineCapType, idstrokeStyleLineCapType, idstrokeStyleButtCap ); 

            var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" ); 

            var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" ); 

            var idstrokeStyleMiterJoin = stringIDToTypeID( "strokeStyleMiterJoin" ); 

            desc19.putEnumerated( idstrokeStyleLineJoinType, idstrokeStyleLineJoinType, idstrokeStyleMiterJoin ); 

            var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" ); 

            var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" ); 

            var idstrokeStyleAlignInside = stringIDToTypeID( "strokeStyleAlignInside" ); 

            desc19.putEnumerated( idstrokeStyleLineAlignment, idstrokeStyleLineAlignment, idstrokeStyleAlignInside ); 

            var idstrokeStyleScaleLock = stringIDToTypeID( "strokeStyleScaleLock" ); 

            desc19.putBoolean( idstrokeStyleScaleLock, false ); 

            var idstrokeStyleStrokeAdjust = stringIDToTypeID( "strokeStyleStrokeAdjust" ); 

            desc19.putBoolean( idstrokeStyleStrokeAdjust, false ); 

            var idstrokeStyleLineDashSet = stringIDToTypeID( "strokeStyleLineDashSet" ); 

                var list2 = new ActionList(); 

            desc19.putList( idstrokeStyleLineDashSet, list2 ); 

            var idstrokeStyleBlendMode = stringIDToTypeID( "strokeStyleBlendMode" ); 

            var idBlnM = charIDToTypeID( "BlnM" ); 

            var idNrml = charIDToTypeID( "Nrml" ); 

            desc19.putEnumerated( idstrokeStyleBlendMode, idBlnM, idNrml ); 

            var idstrokeStyleOpacity = stringIDToTypeID( "strokeStyleOpacity" ); 

            var idPrc = charIDToTypeID( "#Prc" ); 

            desc19.putUnitDouble( idstrokeStyleOpacity, idPrc, 100.000000 ); 

            var idstrokeStyleContent = stringIDToTypeID( "strokeStyleContent" ); 

                var desc20 = new ActionDescriptor(); 

                var idClr = charIDToTypeID( "Clr " ); 

                    var desc21 = new ActionDescriptor(); 

                    var idRd = charIDToTypeID( "Rd  " ); 

                    desc21.putDouble( idRd, 121.991998 ); 

                    var idGrn = charIDToTypeID( "Grn " ); 

                    desc21.putDouble( idGrn, 121.991998 ); 

                    var idBl = charIDToTypeID( "Bl  " ); 

                    desc21.putDouble( idBl, 121.991998 ); 

                var idRGBC = charIDToTypeID( "RGBC" ); 

                desc20.putObject( idClr, idRGBC, desc21 ); 

            var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" ); 

            desc19.putObject( idstrokeStyleContent, idsolidColorLayer, desc20 ); 

            var idstrokeStyleResolution = stringIDToTypeID( "strokeStyleResolution" ); 

            desc19.putDouble( idstrokeStyleResolution, 300.000000 ); 

        var idstrokeStyle = stringIDToTypeID( "strokeStyle" ); 

        desc15.putObject( idstrokeStyle, idstrokeStyle, desc19 ); 

    var idcontentLayer = stringIDToTypeID( "contentLayer" ); 

    desc14.putObject( idUsng, idcontentLayer, desc15 ); 

    var idLyrI = charIDToTypeID( "LyrI" ); 

    desc14.putInteger( idLyrI, 27 ); 

executeAction( idMk, desc14, DialogModes.NO ); 

    };   

mauricior6328708
Inspiring
January 31, 2019

What a fright! Good thing it worked perfectly well now ... I'm very grateful to you one more time Chuck Uebele

Thank you.

Chuck Uebele
Community Expert
Community Expert
January 31, 2019

When I wrote that code, I just made it work with equal number of columns of rows and numbers. So I used the row variable for calculating the columns, so you need to change that.

mauricior6328708
Inspiring
January 31, 2019

It is essential that there be spaces between rows and columns, I also need to fit correctly in the selection when when row and column amount are different. It is possible? How do I fix it?

Thank you