3 Replies Latest reply on Jan 14, 2018 2:03 PM by arteangelus

    Create Grid Rectangles inside selected area

    arteangelus Level 1

      Hello,

      I want to divide selected area into a specified number of rows/columns and place a rectangle shape inside each grid unit.

      I am very newbie to scripting so please help me:)

      sample.jpg

       

      app.bringToFront(); 
      cTID = function(s) { return app.charIDToTypeID(s); };
      sTID = function(s) { return app.stringIDToTypeID(s); };
      
      
      // ensure that there is at least one document open; if not, display error message 
      if (documents.length == 0) {  alert("There are no documents open.");  }
      var doc = app.activeDocument; 
      
      var row = 3;
      var column = 3;
      var l = doc.activeLayer;
      
      function hasSelection(doc) { var res = false;var as = doc.activeHistoryState; doc.selection.deselect(); if (as != doc.activeHistoryState) { res = true; doc.activeHistoryState = as; } return res; }; 
      if (!hasSelection(doc) ) { alert ("Document has no selection"); }
      
      function GetSelectionBounds (doc) { 
      var l = doc.artLayers.add(); 
      
      doc.selection.fill(app.foregroundColor); 
      
      var bnds = l.bounds; 
      var hs = doc.historyStates; 
      if (hs[hs.length-2].name == "Layer Order") { 
      doc.activeHistoryState = hs[hs.length-4]; 
      } else { 
      doc.activeHistoryState = hs[hs.length-3]; 
      } 
      for (var i = 0; i < bnds.length; i++) { 
      bnds[i] = bnds[i].value; 
      } 
      return bnds; 
      }
      // check for a selection 
      function detectSelection() { 
      // declare local variables 
      var userHistory = activeDocument.activeHistoryState; 
      var isSelection = false; 
      }
      var aSelSize = GetSelectionBounds(doc); 
      var width = aSelSize[2] - aSelSize[0]; 
      var height = aSelSize[3] - aSelSize[1]; 
      var wstart = aSelSize[2];
      var wend = aSelSize[0];
      var hstart = aSelSize[3];
      var hend = aSelSize[1];
      singlewidth = width /column;
      singleheight = height /row;
      p1 = [0,0];
      p2= [singlewidth,0];
      p3 = [singlewidth,singleheight];
      p4 = [0,singleheight];
      var docwidth  =  app.activeDocument.width;
      var docheight =  app.activeDocument.height;
      
      function movelayer(width,height ) {
      
      
          var desc1 = new ActionDescriptor();
          var ref1 = new ActionReference();
          ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
          desc1.putReference(cTID('null'), ref1);
          desc1.putEnumerated(cTID('FTcs'), cTID('QCSt'), sTID("QCSSide3"));
          var desc2 = new ActionDescriptor();
          desc2.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'),width); // Width 
          desc2.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), height); // Height
          desc1.putObject(cTID('Ofst'), cTID('Ofst'), desc2);
          desc1.putEnumerated(cTID('Intr'), cTID('Intp'), cTID('Bcbc'));
          executeAction(cTID('Trnf'), desc1,  DialogModes.NO);
        };
          
      // Duplicate with Layer Name
      
      
      function duplicatelayer(duplicatename) {
          var desc1 = new ActionDescriptor();
          var ref1 = new ActionReference();
          ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
          desc1.putReference(cTID('null'), ref1);
          desc1.putString(cTID('Nm  '), duplicatename);
          desc1.putInteger(cTID('Vrsn'), 5);
          executeAction(cTID('Dplc'), desc1, DialogModes.NO);
        };
      function Create_Rectangle(width,height) {
      
      
          var desc1 = new ActionDescriptor();
          var ref1 = new ActionReference();ref1.putClass(sTID("contentLayer"));desc1.putReference(cTID('null'), ref1);
          var desc2 = new ActionDescriptor();var desc3 = new ActionDescriptor();var desc4 = new ActionDescriptor();
          desc4.putDouble(cTID('Rd  '), 0); // R
          desc4.putDouble(cTID('Grn '), 0); // G
          desc4.putDouble(cTID('Bl  '), 0); // B
          desc3.putObject(cTID('Clr '), sTID("RGBColor"), desc4);
          desc2.putObject(cTID('Type'), sTID("solidColorLayer"), desc3);
          var desc5 = new ActionDescriptor();
          desc5.putInteger(sTID("unitValueQuadVersion"), 1);
          desc5.putUnitDouble(cTID('Top '), cTID('#Pxl'), 0);
          desc5.putUnitDouble(cTID('Left'), cTID('#Pxl'), 0);
          desc5.putUnitDouble(cTID('Btom'), cTID('#Pxl'), width);
          desc5.putUnitDouble(cTID('Rght'), cTID('#Pxl'), height);
          desc5.putUnitDouble(sTID("topRight"), cTID('#Pxl'), 0);
          desc5.putUnitDouble(sTID("topLeft"), cTID('#Pxl'), 0);
          desc5.putUnitDouble(sTID("bottomLeft"), cTID('#Pxl'), 0);
          desc5.putUnitDouble(sTID("bottomRight"), cTID('#Pxl'), 0);
          desc2.putObject(cTID('Shp '), cTID('Rctn'), desc5);
          var desc6 = new ActionDescriptor();
          desc6.putInteger(sTID("strokeStyleVersion"), 2);
          desc6.putBoolean(sTID("strokeEnabled"), false);
          desc6.putBoolean(sTID("fillEnabled"), true);
          desc6.putUnitDouble(sTID("strokeStyleLineWidth"), cTID('#Pxl'), 1);
          desc6.putUnitDouble(sTID("strokeStyleLineDashOffset"), cTID('#Pnt'), 0);
          desc6.putDouble(sTID("strokeStyleMiterLimit"), 100);
          desc6.putEnumerated(sTID("strokeStyleLineCapType"), sTID("strokeStyleLineCapType"), sTID("strokeStyleButtCap"));
          desc6.putEnumerated(sTID("strokeStyleLineJoinType"), sTID("strokeStyleLineJoinType"), sTID("strokeStyleMiterJoin"));
          desc6.putEnumerated(sTID("strokeStyleLineAlignment"), sTID("strokeStyleLineAlignment"), sTID("strokeStyleAlignInside"));
          desc6.putBoolean(sTID("strokeStyleScaleLock"), false);
          desc6.putBoolean(sTID("strokeStyleStrokeAdjust"), false);
          var list1 = new ActionList();
          desc6.putList(sTID("strokeStyleLineDashSet"), list1);
          desc6.putEnumerated(sTID("strokeStyleBlendMode"), cTID('BlnM'), cTID('Nrml'));
          desc6.putUnitDouble(sTID("strokeStyleOpacity"), cTID('#Prc'), 100);
          var desc7 = new ActionDescriptor();
          var desc8 = new ActionDescriptor();
          desc8.putDouble(cTID('Rd  '), 1);
          desc8.putDouble(cTID('Grn '), 1);
          desc8.putDouble(cTID('Bl  '), 1);
          desc7.putObject(cTID('Clr '), sTID("RGBColor"), desc8);
          desc6.putObject(sTID("strokeStyleContent"), sTID("solidColorLayer"), desc7);
          desc6.putDouble(sTID("strokeStyleResolution"), 72);
          desc2.putObject(sTID("strokeStyle"), sTID("strokeStyle"), desc6);
          desc1.putObject(cTID('Usng'), sTID("contentLayer"), desc2);
          desc1.putInteger(cTID('LyrI'), 4);
          executeAction(cTID('Mk  '), desc1, DialogModes.NO);
      };
      
      function main(){  
      
      app.displayDialogs = DialogModes.NO;  
      var strtRulerUnits = app.preferences.rulerUnits;  
      var strtTypeUnits = app.preferences.typeUnits;  
      app.preferences.rulerUnits = Units.PIXELS;  
      app.preferences.typeUnits = TypeUnits.PIXELS;  
      activeDocument.selection.deselect();  
      //
      var sh = singleheight;
      var hs = hstart;
      
      
      for (r= 0; r < row; r++)         {
          for (c= 0; c < column; c++)         {
              Create_Rectangle((singleheight),singlewidth);
              doc.activeLayer.name = 'Test';
              movex = ( (wstart + (singlewidth * c) ) - docwidth ) * c;
              movey = hs;
              movelayer ( movex,movey) 
              }
              
              c=0;
             hs = hs + singleheight;
      }
      
      
      }
      main();  
      
        • 1. Re: Create Grid Rectangles inside selected area
          Chuck Uebele Adobe Community Professional & MVP

          Try this:

           

          #target photoshop
          
          var row = 3;  
          var column = 3;  
          
          if(app.documents.length>0){
              var doc = activeDocument;
              try{
                  var sBounds = doc.selection.bounds;
                  
                  var sWidth = (parseFloat(sBounds[2]) -parseFloat(sBounds[0]))/column;
                  var sHeight = (parseFloat(sBounds[3]) -parseFloat(sBounds[1]))/row;
                  
                  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)
                          leftSide += sWidth
                          }
                      topSide += sHeight
                      }
                  
                  }//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 );
              };  
          
          3 people found this helpful
          • 2. Re: Create Grid Rectangles inside selected area
            geppettol66959005 Level 2

            Chuck Uebele

            A nice job

            I added a simple thing prompt

            so he decides how many shapes he wants to insert

            in the selection.

             

            var row =prompt( "SHAPE ROW N°", 3);
            var column = prompt( "SHAPE COLUMN N°", 3);   
              
            if(app.documents.length>0){  
                var doc = activeDocument;  
                try{  
                    var sBounds = doc.selection.bounds;  
                      
                    var sWidth = (parseFloat(sBounds[2]) -parseFloat(sBounds[0]))/column;  
                    var sHeight = (parseFloat(sBounds[3]) -parseFloat(sBounds[1]))/row;  
                      
                    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)  
                            leftSide += sWidth  
                            }  
                        topSide += sHeight  
                        }  
                      
                    }//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 people found this helpful
            • 3. Re: Create Grid Rectangles inside selected area
              arteangelus Level 1

              Hi Chuck Uebele and geppettol66959005 thank you for your answers,

               

              I have tried to add couple of things such as;

              Convert initially created rectangle into a smart object.

              Add center guides to smart object.

              Duplicate smart object through selected area's grid.

              Add additional guides in direction of guides.

               

              *Adding margin option would be great but it is too complicated for me right now;

               

               

              #target photoshop
              var row =prompt( "SHAPE ROW N°", 3);  
              var column = prompt( "SHAPE COLUMN N°", 3);     
              
              
              cTID = function(s) { return app.charIDToTypeID(s); };
              sTID = function(s) { return app.stringIDToTypeID(s); };
              
              
              var sWidth,sHeight,leftSide,topSide;
              if (documents.length == 0) { 
              alert("There are no documents open."); 
              } 
              
              
              else if (!detectSelection()) { 
              alert("You must first create a selection."); 
              } 
              
              
              else {
                  
                  var doc = activeDocument;    
                 
                      var sBounds = doc.selection.bounds;    
                      sWidth = (parseFloat(sBounds[2]) -parseFloat(sBounds[0]))/column;    
                      sHeight = (parseFloat(sBounds[3]) -parseFloat(sBounds[1]))/row;    
                      guideLine(parseFloat(sBounds[1]), 'Hrzn');
                      guideLine(parseFloat(sBounds[3]), 'Hrzn');
                      guideLine(sWidth, 'Hrzn');
                      guideLine(parseFloat(sBounds[0]), 'Vrtc');
                      guideLine(parseFloat(sBounds[2]), 'Vrtc');
                      guideLine(sHeight, 'Vrtc');
                      
                      var singlew = sWidth;
                      var singleh = sHeight; 
                      leftSide = parseFloat (sBounds[0]);
                      topSide = parseFloat (sBounds[1]);    
                      Create_Rectangle (topSide, leftSide, topSide + sHeight, leftSide + sWidth)
                      Convert_to_Smart_Object(); // Convert to Smart Object
                      editlayer();
                      var rr =0;
                      var totalnumber = 1;
                      doc.activeLayer.name =  'Square_' +totalnumber; 
                      totalnumber++;
                      var gox = 0;
                      var goy = 0;
                      
                      for(var r=0;r<row;r++){
                              guidehor = ( parseFloat  (sBounds[1]) )    +  (singlew * (r+1) );
                              guideLine(  guidehor, 'Vrtc');
              
              
                          for (var c=0;c<column;c++){
                             guidehor = ( parseFloat  (sBounds[0]) )    +  (singlew * (c+1) );
                              guideLine(  guidehor, 'Hrzn');
              
              
                              var newLayer  = doc.activeLayer.duplicate();
                              newLayer.name = 'Square_' +totalnumber;
                              doc.activeLayer = doc.artLayers.getByName('Square_' +totalnumber);
                              gox= singlew;
                              goy = 0;
                              move(gox,goy);
                              totalnumber++;
                              
                              }
                               goy= singleh;
                               move( ((-singlew*(column-1))-singlew),goy);
                      }
              doc.activeLayer.remove();
              }
              
              
              function move(x,y) {
                  var desc1 = new ActionDescriptor();
                  desc1.putEnumerated(cTID('FTcs'), cTID('QCSt'), sTID("QCSCorner0"));
                  var desc2 = new ActionDescriptor();
                  desc2.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'), x);
                  desc2.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), y);
                  desc1.putObject(cTID('Ofst'), cTID('Ofst'), desc2);
                  executeAction(cTID('Trnf'), desc1, DialogModes.NO);
                };
              
              function detectSelection(){ 
              // declare local variables 
              var userHistory = activeDocument.activeHistoryState; 
              var isSelection = false; 
              
              
              // try Selection, Modify, Border and then undo if selection is detected 
              try { 
              activeDocument.selection.selectBorder(1); 
              activeDocument.activeHistoryState = userHistory; 
              isSelection = true; 
              } 
              // catch errors - no selection detected 
              catch (e) {} 
              return isSelection; 
              } 
              function guideLine(position, type) { 
                 var id296 = charIDToTypeID( "Mk  " ); 
                     var desc50 = new ActionDescriptor(); 
                     var id297 = charIDToTypeID( "Nw  " ); 
                         var desc51 = new ActionDescriptor(); 
                         var id298 = charIDToTypeID( "Pstn" ); 
                         var id299 = charIDToTypeID( "#Pxl" ); 
                         desc51.putUnitDouble( id298, id299, position ); 
                         var id300 = charIDToTypeID( "Ornt" ); 
                         var id301 = charIDToTypeID( "Ornt" ); 
                         var id302 = charIDToTypeID( type ); 
                        desc51.putEnumerated( id300, id301, id302 ); 
                        var id303 = charIDToTypeID( "Gd  " ); 
                     desc50.putObject( id297, id303, desc51 ); 
                 executeAction( id296, desc50, DialogModes.NO ); 
              };
              
              
              function undo() {
                 executeAction(cTID("undo", undefined,
              DialogModes.NO));
              };
              
              
              
              
              function editlayer() {
                // Edit Contents
                function step1(enabled, withDialog) {
                  if (enabled != undefined && !enabled)
                    return;
                  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
                  var desc1 = new ActionDescriptor();
                  executeAction(sTID('placedLayerEditContents'), desc1, dialogMode);
                };
              
              
                // Clear Canvas Guides
                function step2(enabled, withDialog) {
                  if (enabled != undefined && !enabled)
                    return;
                  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
                  executeAction(sTID('clearCanvasGuides'), undefined, dialogMode);
                };
              
              
                // New Guide Layout
                function step3(enabled, withDialog) {
                  if (enabled != undefined && !enabled)
                    return;
                  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
                  var desc1 = new ActionDescriptor();
                  desc1.putEnumerated(sTID("presetKind"), sTID("presetKindType"), sTID("presetKindCustom"));
                  var desc2 = new ActionDescriptor();
                  desc2.putInteger(sTID("colCount"), 2);
                  desc2.putInteger(sTID("rowCount"), 2);
                  desc1.putObject(sTID("guideLayout"), sTID("guideLayout"), desc2);
                  desc1.putEnumerated(sTID("guideTarget"), sTID("guideTarget"), sTID("guideTargetCanvas"));
                  executeAction(sTID('newGuideLayout'), desc1, dialogMode);
                };
              
              
                // Save
                function step4(enabled, withDialog) {
                  if (enabled != undefined && !enabled)
                    return;
                  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
                  executeAction(cTID('save'), undefined, dialogMode);
                };
              
              
                // Close
                function step5(enabled, withDialog) {
                  if (enabled != undefined && !enabled)
                    return;
                  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
                  var desc1 = new ActionDescriptor();
                  desc1.putInteger(cTID('DocI'), 3950);
                  desc1.putBoolean(sTID("forceNotify"), true);
                  executeAction(cTID('Cls '), desc1, dialogMode);
                };
              
              
                step1();      // Edit Contents
                step2();      // Clear Canvas Guides
                step3();      // New Guide Layout
                step4();      // Save
                step5();      // Close
              };
              
              
              
              
              
              
              function Convert_to_Smart_Object(enabled, withDialog) {
                
                  if (enabled != undefined && !enabled)
                    return;
                  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
                  executeAction(sTID('newPlacedLayer'), undefined, dialogMode);
                };
              function getSelectedLayers(doc) {
                var selLayers = [];
              
                  var layers = doc.activeLayer;
              
                for (var i = 0; i < layers.length; i++) {
                  selLayers.push(layers[i]);
                }}
              
              
              
              
              
              
              
              function Create_Rectangle(top,left,bottom,right) {    
              
              
                  var desc1 = new ActionDescriptor();
                  var ref1 = new ActionReference();
                  ref1.putClass(sTID("contentLayer"));
                  desc1.putReference(cTID('null'), ref1);
                  var desc2 = new ActionDescriptor();
                  var desc3 = new ActionDescriptor();
                  var desc4 = new ActionDescriptor();
                  desc4.putDouble(cTID('Rd  '), 0);
                  desc4.putDouble(cTID('Grn '), 0);
                  desc4.putDouble(cTID('Bl  '), 0);
                  desc3.putObject(cTID('Clr '), sTID("RGBColor"), desc4);
                  desc2.putObject(cTID('Type'), sTID("solidColorLayer"), desc3);
                  var desc5 = new ActionDescriptor();
                  desc5.putInteger(sTID("unitValueQuadVersion"), 1);
                  desc5.putUnitDouble(cTID('Top '), cTID('#Pxl'), top); // Top
                  desc5.putUnitDouble(cTID('Left'), cTID('#Pxl'), left); // Left
                  desc5.putUnitDouble(cTID('Btom'), cTID('#Pxl'), bottom); // Bottom
                  desc5.putUnitDouble(cTID('Rght'), cTID('#Pxl'), right); // Right
                  desc5.putUnitDouble(sTID("topRight"), cTID('#Pxl'), 0);
                  desc5.putUnitDouble(sTID("topLeft"), cTID('#Pxl'), 0);
                  desc5.putUnitDouble(sTID("bottomLeft"), cTID('#Pxl'), 0);
                  desc5.putUnitDouble(sTID("bottomRight"), cTID('#Pxl'), 0);
                  desc2.putObject(cTID('Shp '), cTID('Rctn'), desc5);
                  var desc6 = new ActionDescriptor();
                  desc6.putInteger(sTID("strokeStyleVersion"), 2);
                  desc6.putBoolean(sTID("strokeEnabled"), false);
                  desc6.putBoolean(sTID("fillEnabled"), true);
                  desc6.putUnitDouble(sTID("strokeStyleLineWidth"), cTID('#Pxl'), 0);
                  desc6.putUnitDouble(sTID("strokeStyleLineDashOffset"), cTID('#Pnt'), 0);
                  desc6.putDouble(sTID("strokeStyleMiterLimit"), 100);
                  desc6.putEnumerated(sTID("strokeStyleLineCapType"), sTID("strokeStyleLineCapType"), sTID("strokeStyleButtCap"));
                  desc6.putEnumerated(sTID("strokeStyleLineJoinType"), sTID("strokeStyleLineJoinType"), sTID("strokeStyleMiterJoin"));
                  desc6.putEnumerated(sTID("strokeStyleLineAlignment"), sTID("strokeStyleLineAlignment"), sTID("strokeStyleAlignInside"));
                  desc6.putBoolean(sTID("strokeStyleScaleLock"), false);
                  desc6.putBoolean(sTID("strokeStyleStrokeAdjust"), false);
                  var list1 = new ActionList();
                  desc6.putList(sTID("strokeStyleLineDashSet"), list1);
                  desc6.putEnumerated(sTID("strokeStyleBlendMode"), cTID('BlnM'), cTID('Nrml'));
                  desc6.putUnitDouble(sTID("strokeStyleOpacity"), cTID('#Prc'), 100);
                  var desc7 = new ActionDescriptor();
                  var desc8 = new ActionDescriptor();
                  desc8.putDouble(cTID('Rd  '), 0.10933211960946);
                  desc8.putDouble(cTID('Grn '), 0.10933211960946);
                  desc8.putDouble(cTID('Bl  '), 0.10933211960946);
                  desc7.putObject(cTID('Clr '), sTID("RGBColor"), desc8);
                  desc6.putObject(sTID("strokeStyleContent"), sTID("solidColorLayer"), desc7);
                  desc6.putDouble(sTID("strokeStyleResolution"), 72);
                  desc2.putObject(sTID("strokeStyle"), sTID("strokeStyle"), desc6);
                  desc1.putObject(cTID('Usng'), sTID("contentLayer"), desc2);
                  desc1.putInteger(cTID('LyrI'), 258);
                  executeAction(cTID('Mk  '), desc1,  DialogModes.NO);
              
              
              
              
              };