5 Replies Latest reply on Apr 16, 2011 6:00 AM by JJMack

    Selection without selection bounds

    Ghoulfool Level 1

      I'm using CS2 so I can't call on selection bounds (which seems to be the preferred method of working with selections)

       

      var sb = activeDocument.selection.bounds;
      var sbWidth = sb[2]-sb[0];
      var sbHeight = sb[3]-sb[1];

      alert(sbWidth + " , " + sbHeight)

       

      So the above code won't work for me

       

      How can I find out (1) if a selection is set (selection width & height = 0?) and (2) find the width and height of said selection assuming that only the rectangular marquee is used (not ellipse or lasso).

       

      Cheers

        • 1. Re: Selection without selection bounds
          Paul Riggott Level 6

          Here are a couple of x's functions that work with CS2...

           

          function hasSelection (doc) { 
          if(doc == undefined) doc = activeDocument;
            var res = false; 
            var as = doc.activeHistoryState; 
            doc.selection.deselect(); 
            if (as != doc.activeHistoryState) { 
              res = true; 
              doc.activeHistoryState = as; 
            } 
            return res; 
          };
          

           

          getSelectionBounds = function(doc) { 
              if(doc == undefined) doc = activeDocument;
              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; 
          } 
          
          • 2. Re: Selection without selection bounds
            Ghoulfool Level 1

            Any way round that without using history states, just out of interest?

            • 3. Re: Selection without selection bounds
              Paul Riggott Level 6

              Not that I know of.

              • 4. Re: Selection without selection bounds
                xbytor2 Level 4

                From xtools, this works as far back as PS7:

                 

                Stdlib.computeSelectionBoundsPS7 = function(doc) {
                  var bnds = [];
                
                  Stdlib.makeWorkPath(doc);
                
                  try {
                    var pis = Stdlib.getPathItems(doc);
                
                    for (var i = 0; i < pis.count; i++) {
                      var spis = pis.getObjectValue(i).getList(sTID("subpathListKey"));
                      var pps = spis.getObjectValue(0).getList(sTID('points'));
                
                      for (var j = 0; j < pps.count; j++) {
                        var anchorObj = pps.getObjectValue(j).getObjectValue(sTID("anchor"));
                        var anchor = [anchorObj.getUnitDoubleValue(sTID('horizontal')),
                                      anchorObj.getUnitDoubleValue(sTID('vertical'))];
                        if (bnds.length == 0) {
                          bnds[0] = bnds[2] = anchor[0];
                          bnds[1] = bnds[3] = anchor[1];
                        } else {
                          if (anchor[0] < bnds[0]) {
                            bnds[0] = anchor[0];
                          } else if (anchor[0] > bnds[2]) {
                            bnds[2] = anchor[0];
                          }
                          if (anchor[1] < bnds[1]) {
                            bnds[1] = anchor[1];
                          } else if (anchor[1] > bnds[3]) {
                            bnds[3] = anchor[1];
                          }
                        }
                      }
                    }
                  } finally {
                    Stdlib.undo();
                  }
                
                  return bnds;
                };
                

                 

                 

                Not exactly prettty, but it does/did work.

                • 5. Re: Selection without selection bounds
                  JJMack Most Valuable Participant

                  I had to find out bounds of selection that could be any shape for my Photoshop Photo Collage Toolkit the selections are image size and shaped alpha channels in the Photo Collage Template PSD file with the names "Image 1", "Image 2", ..., "Image n".  I had a heck of a time to come up with a way that worked in CS2, CS3, Cs4 and CS5. After consulting with my good friend Rube Goldberg we can up with a contraption that seems to work. This is not a thing of beauty. Some local functions are not shown for the function name states what it does.

                   

                  // Get Alpha Channel's width and height Selection Bounds did not work???

                  makeLayer(); // Make Temp Work Layer

                  loadAlpha("Image " + imageNumber ); // Load Image Alpha Channel

                  fillBlack();

                  activeDocument.selection.invert(); // Inverse

                  // If image size equals canvas size no pixels will be selected clear will fail

                  try{

                  activeDocument.selection.clear(); // One clear did not work

                  activeDocument.selection.clear(); // Two did the trick

                  }catch(e){}

                  activeDocument.selection.deselect(); // Deselect

                  var SB = activeDocument.activeLayer.bounds; // Get the bounds of the work layer

                  var SWidth = (SB[2].value) - (SB[0].value); // Area width

                  var SHeight = (SB[3].value) - (SB[1].value); // Area height

                  activeDocument.activeLayer.remove(); // Remove Work layer

                   

                  Where there is a will there is a way. Layer bounds does work...

                  I needed these selections bounds so I could resize and align placed images to these mapping Alpha Channels and mask any Image overflow.

                  Link to my Photo Collage Toolkit [ JJMack ]