3 Replies Latest reply on Mar 10, 2010 6:06 AM by muyshangai

    Selection coordinates


      Hi. I need to have the coordinates of a selection area (not rectangular one). I've searched the net and only found this script:

                                      doc.Selection.MakeWorkPath(1);// make the path 
                                      PathItem p = doc.PathItems["Work Path"];
                                      for (int u = 1; u <= p.SubPathItems.Count; u++)
                                          for (int t = 1; t <= p.SubPathItems[u].PathPoints.Count; t++)
                                              PathPoint w = p.SubPathItems[u].PathPoints[t];
                                              object[] my = ((object[])w.Anchor);
                                               int myx = Convert.ToInt32(my[0]);
                                              int myy = Convert.ToInt32(my[1]);


      but it's not working as I have no experience with PS scripts. I've saved it as .jsx and tried to load it but it throws errors like it can't set a variable  of the "PathItem" type and such. The script is obviously not completed, after it executes i need a way to obtain the outputted coordinates (text file, area, output window..)


      Can anyone help me out ?


      Thanks beforehand

        • 1. Re: Selection coordinates
          #target Photoshop
          var data=mapa_seleccion(activeDocument,data)
          function mapa_seleccion(doc,out){
               var hs = doc.historyStates;
               preferences.rulerUnits = Units.PIXELS;
               var p = doc.pathItems["Work Path"];
               var a = new Array();
               var t = 0;
               var l = p.subPathItems.length;
               for ( k = 0; k < l; k++) {
                    var n = p.subPathItems[k].pathPoints.length;
                    a[k] = new Array (n);
                    for ( i = 0; i < n; i++){
                         a[k][i] = new Array();
               var t = 0;
               for ( k = 0; k < l; k++) {
                    var n = a[k].length;
                    var s = 0;
                    for ( i = 0; i < n; i++){
                         var j = (i+1)%n;
               doc.activeHistoryState = hs[hs.length-2];
               out=new Array()
               out.area=parseFloat(t/doc.resolution/doc.resolution*2.54*2.54).toFixed(2)//convierte a cm2
               return out
          //You can get the area of the selection with:
          //The "selection" is an array, so you can get it like:
          data.mapa[0] // for the first subpathitem
          data.mapa[0][0] // for the first pathpoint in the first subpathitem, etc.
          • 2. Re: Selection coordinates
            Michael L Hale Level 5

            I'm not sure where you found that code but it looks like whoever wrote it also doesn't have much experence with ExtendScript.


            var doc = app.activeDocument;
            var points = [];//make an array to hold points
            doc.selection.makeWorkPath(1);// make the path 
            var p = doc.pathItems["Work Path"];
            for (var u = 0; u < p.subPathItems.length; u++){
                 for (var t = 0; t < p.subPathItems[u].pathPoints.length; t++){
                      var point = p.subPathItems[u].pathPoints[t];                            
                      var myAnchor = point.anchor;
                      var myx = Math.round(myAnchor[0]);
                      var myy = Math.round(myAnchor[1]);
            alert('There are '+points.length+' points.\rThe First point is at '+points[0] );
            1 person found this helpful
            • 3. Re: Selection coordinates
              SteeleRBG Level 1

              Wow, thanks, it's working