21 Replies Latest reply on Apr 21, 2018 2:09 AM by til5

    Get Layer Transform X Y Top Left Coordinate Position

    X-Raym Level 1

      Hi !

       

      I try to look for this but in fact it seems more complicated than expected, and I didn't find a working up to date solution.

       

      So we have layer bound property, which can return top left value of a layer.

       

      But if the layer is smart layer (or a text) this may not work as I would have expect: indeed, it will return the value of the position of the first pixel, and not the actual smart layer position (determined by it's original boundaries).

       

      Here is a screen capture to show the difference between the layer top left coordinate and the transform X Y position (which match the original file placement):

       

       

      How do you get these X Y positions from a Photoshop script ?

       

      Thanks !

        • 2. Re: Get Layer Transform X Y Top Left Coordinate Position
          X-Raym Level 1

          r-bin

          Hi, thanks for your very quick assistance (and your initial code snippet) !

           

          This get_smart_object_corners() function doesn't take any parameter, how come ?

          How to pass a Layer as parameter to it ?

           

          My ultimate goal is to merge this code snippet to

           

          Adobe-Export-Scripts/Export Layer Coordinates Photoshop.jsx at master · bronzehedwick/Adobe-Export-Scripts · GitHub

           

          As you can see L44, the function need to take a Layer as parameter.

           

          Many thanks again!

           

          (let me know if you prefer me to answer in the original thread).

          • 3. Re: Get Layer Transform X Y Top Left Coordinate Position
            r-bin Level 4

            Try this function

             

            function get_layer_bounds(layer)
                {
                try
                    {
                    if (layer.kind == LayerKind.SMARTOBJECT)
                        {
                        var layer0 = activeDocument.activeLayer;
                        activeDocument.activeLayer = layer;
            
                        var r = new ActionReference();
                        r.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
            
                        var d = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObjectMore")).getList(stringIDToTypeID("transform"));
            
                        var x = [d.getDouble(0), d.getDouble(2), d.getDouble(4), d.getDouble(6)];
                        var y = [d.getDouble(1), d.getDouble(3), d.getDouble(5), d.getDouble(7)];
                       
                        var l = [Math.min(x[0], Math.min(x[1], Math.min(x[2], x[3]))) ];
                        var r = [Math.max(x[0], Math.max(x[1], Math.max(x[2], x[3]))) ];
            
                        var t = [Math.min(y[0], Math.min(y[1], Math.min(y[2], y[3]))) ];
                        var b = [Math.max(y[0], Math.max(y[1], Math.max(y[2], y[3]))) ];
            
                        activeDocument.activeLayer = layer0;
            
                        return [ UnitValue(l,"px"), UnitValue(t,"px"), UnitValue(r,"px"), UnitValue(b,"px") ];
                        }
                    else
                        return layer.boundsNoEffects;
                    }
                catch (e) { alert(e); }
                }
            
            1 person found this helpful
            • 4. Re: Get Layer Transform X Y Top Left Coordinate Position
              JJMack Most Valuable Participant

              I have never seen any code posted that can get a smart object layers associated transform setting.    Never seen code to get any of the scaling values.

               

              I see this function the gets the layers pixels bonds  where activeDoctment.activelayer.bounds may return a larger area if the layer has a layer style that extends outside the layers pixel area, like a drop shadow . 

               

              I have seen the code that you posted to get the layer transform four corners to calculate a rotation angle in CC 2018 that may work if the layer transform simply sized and rotated the object  by doing the right triangle math for the slope of a side after getting the layers four coners points.  However that will not be correct if the layers associated transform has some perspective or other distortion.    Also when I tested the function. The function did not return the transform's four corners.

               

              I have never seen code that can get the values that Photoshop shows in the transform tools option bar.  Fot the smart object layers assocated transform.

               

              Capture.jpg]

              1 person found this helpful
              • 5. Re: Get Layer Transform X Y Top Left Coordinate Position
                r-bin Level 4

                Ok. You can experiment by replacing "transform" with "nonAffineTransform".
                And also explore the "size" and "warp" objects in the smartObjectMore object.

                 

                I do not have time for this now )

                • 6. Re: Get Layer Transform X Y Top Left Coordinate Position
                  JJMack Most Valuable Participant

                  Perhaps you can but that is above my pay grade.  I do not know JavaScript or java objects structures to be able to parse them or process them well. Do not know to retriever some Photoshop information using action manager code.  I only modify Scriptlistener generated Action manager code you know the execute side of Action manger.

                   

                  I'm older and retired I look for code from younger whippersnappers so I can hack on it.   If I have to say it myself,  I will, I may need to use a sledge hammer  but I can hammer on javascript and often get it to do what I want it to do.

                   

                  The first place I look for code is hear searching this forum. And I'm sadden when great scripters like Paul leave when other like Mike pass away while X is still maintaining Image Processor Pro but only drops in here once in a while. In other words I need you to stay here.

                   

                  Thank you now that works well and has given me some other things to think about. It interesting to me  how the layers bounds area and the transform points area ciffer, Don't know if I can make any use out of that.

                  // Select Smart Object Layers Associated transform area  
                  app.activeDocument.selection.select(get_smart_object_corners());
                  ////////////////////////////////////////////////////////////////////////////////////////// //
                  function get_smart_object_corners() {
                      try
                          {
                          var r = new ActionReference();
                          r.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
                          var d;
                          try { d = executeActionGet(r); } catch (e) { alert(e); return; }
                          try { d = d.getObjectValue(stringIDToTypeID("smartObjectMore"));} catch (e) { alert(e); return; }
                          try { d = d.getList(stringIDToTypeID("nonAffineTransform"));} catch (e) { alert(e); return; }
                          var ret = [[d.getDouble(0),d.getDouble(1)],
                                     [d.getDouble(2),d.getDouble(3)],
                                     [d.getDouble(4),d.getDouble(5)],
                                     [d.getDouble(6),d.getDouble(7)]];
                          return ret;
                          }
                      catch (e) { alert(e); }
                  }
                  
                  1 person found this helpful
                  • 7. Re: Get Layer Transform X Y Top Left Coordinate Position
                    X-Raym Level 1

                    r-bin

                    Many thanks for your expertise on this code snippet !! it seems to works nicely,

                    at least in my simple case (no perspective or complex transform on layer).

                     

                    Without your generous help, it would have been impossible.

                     

                    JJMack

                    Does it works for you for these complex scenarios ?

                    If yes, I'll mark r-bin answer last code snippet as the good one.

                     

                    I agree with you that it is suprising that a simple property like that (it is shown in plain text in some text-input of the UI) needs to deal with such complex function on the script side. That was a very unexpected behavior, as nothing on the documentation explained that position would behave weirdly with smart objects.

                     

                    At least we have a workaround now !

                    • 8. Re: Get Layer Transform X Y Top Left Coordinate Position
                      JJMack Most Valuable Participant

                      If you exclude Transform Warp and I think  you should all the other Transform have the same user interface only their operation differs. How dragging with the mouse function differ.  All record in the history palette as free transform even Transform Warp.  Transform Warp though has a different UI interface and the same interface in the tools options bar there is a little icon that you can to toggle between the two transform UI.  You can use both transforming a layer.  So a transformed smart object transform can have setting for both interfaces.  I would think all smart object layers will have standard free transform settings and  some Smart object layers associated transform may have additional  transform warp settings.  The script seems the get the Free Transform four corners in a sequence that a selection using them will show when extreme distortion was used causing sides to crossover an other,

                      Capture.jpg

                      • 9. Re: Get Layer Transform X Y Top Left Coordinate Position
                        X-Raym Level 1

                        JJMack

                        These are definitely complex cases, where the definition of Top Left XY may no be the same for everyone one

                        I consider this should be part of a wider feature request for getting XY of all Smart Layers corners.

                         

                        I mark r-bin solution as a good one for now (as it does what I was looking for in my case), and I'll update if he or someone else come with a solution which could handle these cases.

                         

                        Thanks for having taken the time to digg these possible issues !

                        • 10. Re: Get Layer Transform X Y Top Left Coordinate Position
                          JJMack Most Valuable Participant

                          The answer you marked correct is not the r-bin script I was writing about.  I think you missed that point. It was his other script to get the four corner points that for me did not work correctly.  r-bin wrote how to change the function, So I change it the way he wrote and tested it. And posted the script.

                          1 person found this helpful
                          • 11. Re: Get Layer Transform X Y Top Left Coordinate Position
                            X-Raym Level 1

                            JJMack

                            It is this function that solves the problem I had and which I implemented in my script,

                            Your code snippet doesn't have Layer as function parameters as I requested in my second post, r-bin post was an answer to that.

                            But yes, your code snippet do contains a part of the solution as well... But I can't mark two posts the "the correct answer". :S

                             

                            I marked your post as "useful" and "recommended" though

                             

                            Thanks again for your contribution on this problem!

                            • 12. Re: Get Layer Transform X Y Top Left Coordinate Position
                              JJMack Most Valuable Participant

                              I just wanted to make sure the you knew the were two scripts r-bin posted and he told me how to make the mod I wanted, So there are three scripts.  I just wanted to make sure you knew that.  I deserve no credit its all r-bin...

                              • 13. Re: Get Layer Transform X Y Top Left Coordinate Position
                                JJMack Most Valuable Participant

                                r-bin  wrote

                                 

                                And also explore the "size" and "warp" objects in the smartObjectMore object.

                                 

                                I do not have time for this now )

                                I you get some time could you code  functions that one could use to explore a smart objects size and warp.  Like you did for  smart object layers object corners.  As I wrote I can hack some but  I lack the knowledge to use  action manager code to retrieve Photoshop layer related data.  I needed you to tell me to change to "nonAffineTransform" to get the corners I wanted.  You knowledge of what Photoshop has and how to get at it is knowledge I do not expect to acquire in my lifetime I'm 77 and slowing down.  Your four corners function help me do some smart object layer exploring. Thank you

                                // Explore Smart Object Layer's 4 corner-points (x,y)
                                /////////////////////////////////////////////////////
                                if (app.activeDocument.activeLayer.kind!=LayerKind.SMARTOBJECT) alert("ActiveLayer not Smart Object Layer");
                                else {
                                var orig_ruler_units = app.preferences.rulerUnits;
                                app.preferences.rulerUnits = Units.PIXELS; // Set the ruler units to PIXELS
                                clearGuides();                              // Clear Canvas Guides
                                var points = get_smart_object_corners();    // Get Smart Object layers corners
                                app.activeDocument.selection.select(points);// Set new selection  using layers corners 
                                    // Mark their (x,y)
                                MarkX( points[0][0]);
                                MarkY( points[0][1]);
                                MarkX( points[1][0]);
                                MarkY( points[1][1]);
                                MarkX( points[2][0]);
                                MarkY( points[2][1]);
                                MarkX( points[3][0]);
                                MarkY( points[3][1]);
                                // get the slope and length of the layers sides 0 is horizontal 90 is vertical
                                var w = points[0][0] - points[1][0];
                                var h = points[0][1] - points[1][1];
                                var l0 = Math.sqrt(Math.pow(w,2)+Math.pow(h,2));
                                var angle0 = Math.atan(h/w) * 180.0 / Math.PI;
                                var w = points[1][0] - points[2][0] ;
                                var h = points[1][1] - points[2][1] ;
                                var l1 = Math.sqrt(Math.pow(w,2)+Math.pow(h,2));
                                var angle1 = Math.atan(h/w) * 180.0 / Math.PI;
                                var w = points[2][0] - points[3][0] ;
                                var h = points[2][1] - points[3][1] ;
                                var l2 = Math.sqrt(Math.pow(w,2)+Math.pow(h,2));
                                var angle2 = Math.atan(h/w) * 180.0 / Math.PI;
                                var w = points[3][0] - points[0][0] ;
                                var h = points[3][1] - points[0][1] ;
                                var l3 = Math.sqrt(Math.pow(w,2)+Math.pow(h,2));
                                var angle3 = Math.atan(h/w) * 180.0 / Math.PI;
                                //prerimmeter length
                                var l4 = l0+l1+l2+l3;
                                    //show the user this information
                                alert( Math.round(points[0][0]) + ", " +  Math.round(points[0][1]) + " "
                                + angle0.](0) + "° " + l0.toFixed(2) +"\n\n"
                                + Math.round(points[1][0]) + ", " +  Math.round(points[1][1]) + " "
                                  + angle1.toFixed(1) + "° " + l1.toFixed(2) +"\n\n"
                                + Math.round(points[2][0]) + ", " +  Math.round(points[2][1]) + " "
                                + angle2.toFixed(1) + "° " + l2.toFixed(2) +"\n\n"
                                + Math.round(points[3][0]) + ", " +  Math.round(points[3][1]) + " "
                                + angle3.toFixed(1) + "° " + l3.toFixed(2) +"\n\n"
                                + l4.toFixed(2));
                                
                                app.preferences.rulerUnits = orig_ruler_units; // Reset units to original settings
                                }
                                ////////////////////////////////////////////////////////////////////////////////////////////////////////
                                // function get_smart_object_corners() returns array from 4 corner-points (x,y) 
                                // of smart object in pixels (value with floating point) https://forums.adobe.com/people/r-bin
                                ////////////////////////////////////////////////////////////////////////////////////////////////////////
                                function get_smart_object_corners() {
                                    try
                                        {
                                        var r = new ActionReference();
                                        r.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
                                        var d;
                                        try { d = executeActionGet(r); } catch (e) { alert(e); return; }
                                        try { d = d.getObjectValue(stringIDToTypeID("smartObjectMore"));    } catch (e) { alert(e); return; }
                                        try { d = d.getList(stringIDToTypeID("nonAffineTransform"));                 } catch (e) { alert(e); return; }
                                        var ret = [[d.getDouble(0),d.getDouble(1)],
                                                   [d.getDouble(2),d.getDouble(3)],
                                                   [d.getDouble(4),d.getDouble(5)],
                                                   [d.getDouble(6),d.getDouble(7)]];
                                        return ret;
                                        }
                                    catch (e) { alert(e); }
                                }
                                function clearGuides() { 
                                   var id556 = charIDToTypeID( "Dlt " ); 
                                       var desc102 = new ActionDescriptor(); 
                                       var id557 = charIDToTypeID( "null" ); 
                                           var ref70 = new ActionReference(); 
                                           var id558 = charIDToTypeID( "Gd  " ); 
                                           var id559 = charIDToTypeID( "Ordn" ); 
                                           var id560 = charIDToTypeID( "Al  " ); 
                                           ref70.putEnumerated( id558, id559, id560 ); 
                                       desc102.putReference( id557, ref70 ); 
                                   executeAction( id556, desc102, DialogModes.NO ); 
                                };
                                
                                
                                function MarkX(x) {
                                // =======================================================
                                var idMk = charIDToTypeID( "Mk  " );
                                    var desc61 = new ActionDescriptor();
                                    var idNw = charIDToTypeID( "Nw  " );
                                        var desc62 = new ActionDescriptor();
                                        var idPstn = charIDToTypeID( "Pstn" );
                                        var idPxl = charIDToTypeID( "#Pxl" );
                                        desc62.putUnitDouble( idPstn, idPxl, x);
                                        var idOrnt = charIDToTypeID( "Ornt" );
                                        var idOrnt = charIDToTypeID( "Ornt" );
                                        var idVrtc = charIDToTypeID( "Vrtc" );
                                        desc62.putEnumerated( idOrnt, idOrnt, idVrtc );
                                    var idGd = charIDToTypeID( "Gd  " );
                                    desc61.putObject( idNw, idGd, desc62 );
                                executeAction( idMk, desc61, DialogModes.NO );
                                }
                                function MarkY(y) {
                                // =======================================================
                                var idMk = charIDToTypeID( "Mk  " );
                                    var desc63 = new ActionDescriptor();
                                    var idNw = charIDToTypeID( "Nw  " );
                                        var desc64 = new ActionDescriptor();
                                        var idPstn = charIDToTypeID( "Pstn" );
                                        var idPxl = charIDToTypeID( "#Pxl" );
                                        desc64.putUnitDouble( idPstn, idPxl, y );
                                        var idOrnt = charIDToTypeID( "Ornt" );
                                        var idOrnt = charIDToTypeID( "Ornt" );
                                        var idHrzn = charIDToTypeID( "Hrzn" );
                                        desc64.putEnumerated( idOrnt, idOrnt, idHrzn );
                                    var idGd = charIDToTypeID( "Gd  " );
                                    desc63.putObject( idNw, idGd, desc64 );
                                executeAction( idMk, desc63, DialogModes.NO );
                                }
                                
                                • 14. Re: Get Layer Transform X Y Top Left Coordinate Position
                                  r-bin Level 4

                                  Well. Here is not a complex code that will give you everything (as I think) values from smartObjectMore.

                                  You decide what to do with them. )

                                   

                                  var r = new ActionReference();  
                                  r.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );  
                                    
                                  var obj = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObjectMore"))
                                  
                                  with (obj)
                                      {
                                      var _tmp;
                                  
                                      var id         = getString(stringIDToTypeID("ID"));
                                      var placed     = getString(stringIDToTypeID("placed"));
                                  
                                      var pageNumber = getInteger(stringIDToTypeID("pageNumber"));
                                      var totalPages = getInteger(stringIDToTypeID("totalPages"));
                                      var crop       = getInteger(stringIDToTypeID("crop"));
                                  
                                      var frameCount    = getInteger(stringIDToTypeID("frameCount"));
                                      var antiAliasType = getInteger(stringIDToTypeID("antiAliasType"));
                                      var type          = getInteger(stringIDToTypeID("type"));
                                  
                                      _tmp = getObjectValue(stringIDToTypeID("frameStep"));
                                      var frameStep = new Object({
                                          numerator  : _tmp.getInteger(stringIDToTypeID("numerator")), 
                                          denominator: _tmp.getInteger(stringIDToTypeID("denominator"))
                                          });
                                  
                                      _tmp = getObjectValue(stringIDToTypeID("duration"));
                                      var duration = new Object({
                                          numerator  : _tmp.getInteger(stringIDToTypeID("numerator")), 
                                          denominator: _tmp.getInteger(stringIDToTypeID("denominator"))
                                          });
                                  
                                      _tmp = getList(stringIDToTypeID("transform"));
                                      var transform  = new Array(); 
                                      for (var i = 0; i < _tmp.count; i+=2) transform.push([_tmp.getDouble(i), _tmp.getDouble(i+1)]); 
                                  
                                      _tmp = getList(stringIDToTypeID("nonAffineTransform"));
                                      var nonAffineTransform  = new Array(); 
                                      for (var i = 0; i < _tmp.count; i+=2) nonAffineTransform.push([_tmp.getDouble(i), _tmp.getDouble(i+1)]); 
                                  
                                      _tmp = getObjectValue(stringIDToTypeID("warp"));
                                  
                                      var warp = new Object({
                                          warpStyle:            typeIDToStringID(_tmp.getEnumerationValue(stringIDToTypeID("warpStyle"))), 
                                          warpValue:            _tmp.getDouble(stringIDToTypeID("warpValue")),
                                          warpPerspective:      _tmp.getDouble(stringIDToTypeID("warpPerspective")),
                                          warpPerspectiveOther: _tmp.getDouble(stringIDToTypeID("warpPerspective")),
                                          warpRotate:           typeIDToStringID(_tmp.getEnumerationValue(stringIDToTypeID("warpRotate"))), 
                                          uOrder:               _tmp.getInteger(stringIDToTypeID("uOrder")),
                                          vOrder:               _tmp.getInteger(stringIDToTypeID("vOrder")),
                                  
                                          bounds: new Object({ 
                                              top   : _tmp.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("top")),
                                              left  : _tmp.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("left")),
                                              bottom: _tmp.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("bottom")),
                                              right : _tmp.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("right")),
                                              }),
                                          });
                                  
                                      warp.meshPoints = new Array();
                                  
                                      try { // may be absent
                                          _tmp = _tmp.getObjectValue(stringIDToTypeID("customEnvelopeWarp")).getList(stringIDToTypeID("meshPoints"));
                                          for (var i = 0; i < _tmp.count; i++) 
                                              {
                                              var x = _tmp.getObjectValue(i);
                                              warp.meshPoints.push([x.getUnitDoubleValue(stringIDToTypeID("horizontal")), x.getUnitDoubleValue(stringIDToTypeID("vertical"))]); 
                                              }
                                          }
                                      catch(e) {}
                                  
                                      _tmp = getObjectValue(stringIDToTypeID("size"));
                                      var size = new Object({
                                          width:  _tmp.getDouble(stringIDToTypeID("width")),
                                          height: _tmp.getDouble(stringIDToTypeID("height")),
                                          });
                                  
                                      var resolution = getUnitDoubleValue(stringIDToTypeID("resolution"));    
                                  
                                      var comp = getInteger(stringIDToTypeID("comp"));    
                                  
                                      _tmp = getObjectValue(stringIDToTypeID("compInfo"));
                                  
                                      var compID = _tmp.getInteger(stringIDToTypeID("compID"));
                                      var originalCompID = _tmp.getInteger(stringIDToTypeID("originalCompID"));
                                  
                                      try { // may be absent
                                         _tmp = _tmp.getList(stringIDToTypeID("compList"));
                                          
                                          var compList = new Array();
                                  
                                          for (var i = 0; i < _tmp.count; i++) 
                                              {
                                              var x = _tmp.getObjectValue(i);
                                  
                                              x.getString (stringIDToTypeID("name"));
                                              x.getInteger(stringIDToTypeID("ID"));
                                              x.getString (stringIDToTypeID("comment"));
                                  
                                              compList.push(new Object({
                                                  name:    x.getString (stringIDToTypeID("name")),
                                                  ID:      x.getInteger(stringIDToTypeID("ID")),
                                                  comment: x.getString (stringIDToTypeID("comment"))
                                                  }));
                                              }
                                          }
                                      catch(e) {}
                                      }
                                  
                                  
                                  // examples
                                  alert (warp.toSource())
                                  alert (size.toSource())
                                  alert (resolution)
                                  
                                  • 15. Re: Get Layer Transform X Y Top Left Coordinate Position
                                    JJMack Most Valuable Participant

                                    Thank You for the Gift

                                    • 16. Re: Get Layer Transform X Y Top Left Coordinate Position
                                      X-Raym Level 1

                                      I just got a very particular cases,

                                       

                                      How would you get X and Y position relative to the Artboard position ? Seems to be a quite complex API case.

                                       

                                      I had a layer on a artboard which was offset by 961px, and all Y were offset by 961px. Idealy, I would prefer to have it not offset.

                                       

                                      Thanks for your help

                                      • 17. Re: Get Layer Transform X Y Top Left Coordinate Position
                                        r-bin Level 4

                                        If the question is to me, then I have never worked with ArtBoards (in CS6 there are none).

                                        But if I understand correctly what you need, then here is a new function which, depending on the parameter "in_artboard" returns the absolute or relative coordinates of the layer (or smart object).

                                         

                                        To work, you also need the function get_prop_value().

                                        its code can be found in this thread.Re: Modifying Levels Adjustment Layer?

                                         

                                        alert(get_layer_bounds(activeDocument.activeLayer, false))
                                        alert(get_layer_bounds(activeDocument.activeLayer, true))
                                        
                                        function get_layer_bounds(layer, in_artboard)  
                                            {  
                                            try  
                                                {  
                                                var ret;
                                        
                                                var layer0 = activeDocument.activeLayer;  
                                                activeDocument.activeLayer = layer;  
                                        
                                                var id = get_prop_value("layer", null, "layerID");
                                        
                                                if (layer.kind == LayerKind.SMARTOBJECT)  
                                                    {  
                                                    var d = get_prop_value("layer", null, "smartObjectMore", "transform");
                                          
                                                    var x = [d.getDouble(0), d.getDouble(2), d.getDouble(4), d.getDouble(6)];  
                                                    var y = [d.getDouble(1), d.getDouble(3), d.getDouble(5), d.getDouble(7)];  
                                                     
                                                    var l = [Math.min(x[0], Math.min(x[1], Math.min(x[2], x[3]))) ];  
                                                    var r = [Math.max(x[0], Math.max(x[1], Math.max(x[2], x[3]))) ];  
                                          
                                                    var t = [Math.min(y[0], Math.min(y[1], Math.min(y[2], y[3]))) ];  
                                                    var b = [Math.max(y[0], Math.max(y[1], Math.max(y[2], y[3]))) ];  
                                          
                                                    ret = [ UnitValue(l,"px"), UnitValue(t,"px"), UnitValue(r,"px"), UnitValue(b,"px") ];  
                                                    }  
                                                else  
                                                    {
                                                    var old_units = app.preferences.rulerUnits;
                                                    app.preferences.rulerUnits = Units.PIXELS;
                                        
                                                    ret = layer.boundsNoEffects;  
                                                    app.preferences.rulerUnits = old_units;
                                                    }
                                        
                                                if (in_artboard)
                                                    {
                                                    var art_rect = get_layer_atrboard_rect(id)
                                            
                                                    ret[0] -= art_rect.left;
                                                    ret[2] -= art_rect.left;
                                        
                                                    ret[1] -= art_rect.top;
                                                    ret[3] -= art_rect.top;
                                                    }
                                        
                                                activeDocument.activeLayer = layer0;  
                                          
                                                return ret;
                                                }  
                                            catch (e) { alert(e); }  
                                            }   
                                        
                                        function get_layer_atrboard_rect(id)
                                            {
                                            try {
                                                var not_artboard_rect = {top:0, left:0, bottom:0, right:0};
                                        
                                                eval("var json = " + get_prop_value("layer", null, "json"));
                                        
                                                if (json == undefined) return not_artboard_rect;
                                        
                                                function test_id(layer, id, n)
                                                    {
                                                    if (layer.id == id) return n;
                                        
                                                    if (layer.layers) 
                                                        {
                                                        for (var i = 0; i < layer.layers.length; i++)
                                                            {
                                                            var ret = test_id(layer.layers[i], id, n+1);
                                                            if (ret != null) return ret;
                                                            }                
                                                        return null;
                                                        }
                                                    else
                                                        { 
                                                        return null;
                                                        }            
                                                    }
                                        
                                                for (var i = 0; i < json.layers.length; i++)
                                                    {
                                                    var ret = test_id(json.layers[i], id, 0);
                                        
                                                    if (ret) 
                                                        {
                                                        if (json.layers[i].artboard) return json.layers[i].artboard.artboardRect; 
                                                        else return not_artboard_rect;
                                                        break;        
                                                        }
                                                    }
                                                
                                                return not_artboard_rect;        
                                                }
                                            catch (e) { alert(e); }
                                            }
                                        
                                        1 person found this helpful
                                        • 18. Re: Get Layer Transform X Y Top Left Coordinate Position
                                          X-Raym Level 1

                                          r-binThe question is for anyone who want to answer Thank you again for your time and expertise !!

                                           

                                          I'll see how this function can be integrated in my current script.

                                           

                                          Cheers !

                                          • 19. Re: Get Layer Transform X Y Top Left Coordinate Position
                                            til5

                                            Hi X-Raym,

                                             

                                            Thanks a lot for starting this thread and thanks everybody else for contributing! This is exactly what I was looking for, too! However, my coding skills are sub-0 by any standard and I wasn't able to merge anything that would work yet. So here's my question for you X-Raym: Did you succeed in your final goal:

                                            My ultimate goal is to merge this code snippet to

                                             

                                            Adobe-Export-Scripts/Export Layer Coordinates Photoshop.jsx at master · bronzehedwick/Adobe-Export-Scripts · GitHub

                                             

                                            If so, would you consider sharing it? That would be super awesome!
                                            Cheers,
                                            Til

                                            • 20. Re: Get Layer Transform X Y Top Left Coordinate Position
                                              JJMack Most Valuable Participant

                                              I believe he did using the layers bound function r-bin posted.  The problem X-Raym having was havng a problem with the way Photoshop works.  When you have Image files where the image has borders that are fully transparent. When you Place these files into to a document or copy the the image to the clipboard and paste the clipboard into a document.  The resulting document layer will have the transparent borders trimmed off.  This can cause problem resizing and position the layer for the layer bounds will be different then the original image canvas. Resizing and position the  layer is complex if the transparent borders were important for the contents positioning.  I always forces these type of image file not to have transparent borders by adding a 1% opacity pixel in the top left and bottom right canvas corners,  r-bin layer bounds function return the Smart object resized canvas bounds instead of the  layers bounds in case any transparent borders were trimmed. 

                                               

                                              I did not test r-bins function out well.  Every smart object layer has an associated transform.  I know r-bins function will work well for smart object layers with transform that simply scale and positions the object.   I did not test the function with smart object layers which had complex associated transform lik warp, distorted, add perspective,  screwed , flipped, etc the object.   r-bin code also only works I believe with Photoshop CC 2015.5 and newer.

                                               

                                              X-Raym did write the r-bin function work for him well but he did not have any smart objects with complex transform.   He may also not have other other objects type.  Like  Vector Objects placed .svg and .ai file.

                                               

                                              From my limited testing all smart object layers seems to work like X-Raym would want. Smart object layers with trimmed borders would look like the borders were not trimmed. The function returns the normal layer bound for layers not smart object layers

                                              1 person found this helpful
                                              • 21. Re: Get Layer Transform X Y Top Left Coordinate Position
                                                til5 Level 1

                                                Hi JJMack ,

                                                 

                                                Thanks for your reply and all the info!
                                                Adding a 1% opacity pixel to top left and bottom right is a pretty clever trick! I might come back to that at some point.
                                                With r-bins instructions I was finally able to put it together (probably pretty obvious to everybody else here in the coding forums, but it made my day). This is the snippets pasted together (didn`t know how to format as code):Re: Get x/y position (and width/height) of the transform box of a layer (not chopped off bounds)

                                                Thanks again for chiming in JJMack and best of luck with your work on those complex smart object transforms!

                                                 

                                                Cheers,
                                                Til