3 Replies Latest reply on Feb 3, 2010 11:33 AM by hilukasz

    resize width of layer to canvas width

    hilukasz Level 1

      I'm having some trouble resizing a layer width to canvas width.

       

      I know one way that works and it involves creating new document, resizing, then pasting back in. This seems extranious it seems there should be an easier way.

          var width = doc.width;
          var bounds = layer.bounds;
          var height = bounds[3]-bounds[1]; \\get height because I want this the same
          var width = doc.width;
          layer.resize (width, height, AnchorPosition.MIDDLECENTER);
      

       

      this seems to fail even though it seems the documentation says it should work? maybe I am misunderstanding.

        • 1. Re: resize width of layer to canvas width
          Michael L Hale Level 5

          I would say either the layer and doc references are not valid or the layer is not visible.

          • 2. Re: resize width of layer to canvas width
            Paul Riggott Level 6

            When dealing with layers the resize is done with percentage, so your

            layer width and height are allways 100% To make it fit the document you need to work out the ratio and then resize.

            This should do what you want....

            main();
            function main(){
                if(!documents.length) return;    
                if (activeDocument.activeLayer.isBackgroundLayer) return;
                fitLayerToDoc();
            }
            function fitLayerToDoc(){   
            var strtRulerUnits = app.preferences.rulerUnits; 
            var strtTypeUnits = app.preferences.typeUnits; 
            app.preferences.rulerUnits = Units.PIXELS; 
            app.preferences.typeUnits = TypeUnits.PIXELS;            
            var docWidth = activeDocument.width.value; 
            var LB = activeDocument.activeLayer.bounds;               
            var LWidth = LB[2].value - LB[0].value;                
            var percentageWidth = ((docWidth/LWidth)*100); 
            activeDocument.activeLayer.resize(percentageWidth,100,AnchorPosition.MIDDLECENTER);
            align( 'AdRg');
            app.preferences.rulerUnits = strtRulerUnits; 
            app.preferences.typeUnits = strtTypeUnits;
            }
            function align(method) { 
            activeDocument.selection.selectAll();
               var desc = new ActionDescriptor();
                       var ref = new ActionReference();
                       ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) ); 
                   desc.putReference( charIDToTypeID( "null" ), ref ); 
                   desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
                try{
               executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO ); 
               }catch(e){}
               activeDocument.selection.deselect();
            }; 
            
            
            • 3. Re: resize width of layer to canvas width
              hilukasz Level 1

              this seems to work however, for some reason it still leaves out a few px on 3 sides when I use vector shape objects. not sure if this is a glitch, but as vector it seems to work perfect.