8 Replies Latest reply on Oct 9, 2015 8:32 AM by JJMack

    Layer patially outside the canvas

    FrankOssie

      How do i get the right dimensions from a layer partially outside the canvas? With the bounds property the width and height are incorrect.

        • 1. Re: Layer patially outside the canvas
          c.pfaffenbichler Level 9

          Sorry, mistook the Forum.

          • 2. Re: Layer patially outside the canvas
            c.pfaffenbichler Level 9

            Does this help?

            #target photoshop

            if (app.documents.length > 0) {

            var ref = new ActionReference();

            ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

            var layerDesc = executeActionGet(ref);

            checkDesc2 (layerDesc.getObjectValue(stringIDToTypeID("bounds")));

            };

            ////// based on code by michael l hale //////

            function checkDesc2 (theDesc) {

            var c = theDesc.count;

            var str = '';

            for(var i=0;i<c;i++){ //enumerate descriptor's keys

              str = str + 'Key '+i+' = '+typeIDToStringID(theDesc.getKey(i))+': '+theDesc.getType(theDesc.getKey(i))+'\n'+getValues (theDesc, i)+'\n';

              };

            alert("desc\n\n"+str);

            };

            ////// check //////

            function getValues (theDesc, theNumber) {

            switch (theDesc.getType(theDesc.getKey(theNumber))) {

            case DescValueType.BOOLEANTYPE:

            return theDesc.getBoolean(theDesc.getKey(theNumber));

            break;

            case DescValueType.CLASSTYPE:

            return theDesc.getClass(theDesc.getKey(theNumber));

            break;

            case DescValueType.DOUBLETYPE:

            return theDesc.getDouble(theDesc.getKey(theNumber));

            break;

            case DescValueType.ENUMERATEDTYPE:

            return (typeIDToStringID(theDesc.getEnumerationValue(theDesc.getKey(theNumber)))+"_"+typeIDToStr ingID(theDesc.getEnumerationType(theDesc.getKey(theNumber))));

            break;

            case DescValueType.INTEGERTYPE:

            return theDesc.getInteger(theDesc.getKey(theNumber));

            break;

            case DescValueType.LISTTYPE:

            return theDesc.getList(theDesc.getKey(theNumber));

            break;

            case DescValueType.OBJECTTYPE:

            return (theDesc.getObjectValue(theDesc.getKey(theNumber))+"_"+typeIDToStringID(theDesc.getObject Type(theDesc.getKey(theNumber))));

            break;

            case DescValueType.REFERENCETYPE:

            return theDesc.getReference(theDesc.getKey(theNumber));

            break;

            case DescValueType.STRINGTYPE:

            return theDesc.getString(theDesc.getKey(theNumber));

            break;

            case DescValueType.UNITDOUBLE:

            return (theDesc.getUnitDoubleValue(theDesc.getKey(theNumber))+"_"+typeIDToStringID(theDesc.getUn itDoubleType(theDesc.getKey(theNumber))));

            break;

            default:

            break;

            };

            };

            • 3. Re: Layer patially outside the canvas
              FrankOssie Level 1

              I'm giving it a try, let you know if it works

              • 4. Re: Layer patially outside the canvas
                FrankOssie Level 1

                Is there an easier way to get the height and width only?

                • 5. Re: Layer patially outside the canvas
                  c.pfaffenbichler Level 9

                  You don’t need to run the whole thing, just get the keys "width" and "height".

                  • 6. Re: Layer patially outside the canvas
                    nvkzNemo Level 2
                    var myLayer = getLayerSize();
                    
                    alert('width = ' + myLayer.width + '\nheight = ' + myLayer.height);
                    
                    
                    function getLayerSize() { 
                        var lyr = {};
                        var ref = new ActionReference();
                        ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
                        try {
                            var bounds = executeActionGet(ref).getObjectValue(stringIDToTypeID("boundsNoEffects"));
                            lyr.height = bounds.getDouble(stringIDToTypeID("height"));
                            lyr.width = bounds.getDouble(stringIDToTypeID("width"));
                        } catch (o) {
                            // fix old Ps (CS6 etc.)
                            var bounds = executeActionGet(ref).getObjectValue(stringIDToTypeID("bounds"));
                            lyr.height = bounds.getDouble(stringIDToTypeID("bottom")) - bounds.getDouble(stringIDToTypeID("top"));
                            lyr.width = bounds.getDouble(stringIDToTypeID("right")) - bounds.getDouble(stringIDToTypeID("left"));
                        }
                        return lyr
                    }
                    
                    
                    
                    • 8. Re: Layer patially outside the canvas
                      JJMack Most Valuable Participant

                      You may also fine that a layer may have sort of two sizes.  Its current size and its actual size.    All layer are not the same.  All smart object layers have an associated transform.  Often this transform is 100% size in which case the current size and actual size are the same.  I ran into this when I was writing scripts to place and position images into a collage template.  Sometimes my code would work perfectly other times not.    What was going on was how Photoshop Place works by default.  By default if the image does not within the canvas Photoshop places the full image in and scales it to fit within the canvas by adjusting the Smart object layers transform scale.  When my code retrieved the layer size it got the layer current size not the layer actual size. So when I calculate the size I needed to resize the layer to my calculation was wrong for I needed to use the layer actual size not its current size.  The solution was easy Before retrieving the layer's  size I would transform the layer to 100% size.  So I would always get the layer actual size for its current size and actual size would now always be the same no matter how the users Photoshop Place resize preference setting was set.

                       

                      You cak also use dom code action manager code is not needed.

                       

                      // enable double-clicking from Mac Finder or Windows Explorer

                      #target photoshop // this command only works in Photoshop CS2 and higher

                      // bring application forward for double-click events

                      app.bringToFront();

                      // Save the current preferences

                      var startRulerUnits = app.preferences.rulerUnits;

                      // Set Photoshop to use pixels

                      app.preferences.rulerUnits = Units.PIXELS;

                      var LB = activeDocument.activeLayer.bounds;

                      var LWidth = (LB[2].value) - (LB[0].value);

                      var LHeight = (LB[3].value) - (LB[1].value);

                      alert("'" + activeDocument.activeLayer.name + "' Layer Bounds\nTop Left " +  LB[0].value + "," +  LB[1].value + "   Bottom Right "  +  LB[2].value + "," +  LB[3].value  + "\nWidth " +  LWidth + "px   Height " + LHeight +"px" );

                      // Return the app preferences

                      app.preferences.rulerUnits = startRulerUnits;