2 Replies Latest reply on May 2, 2017 11:39 AM by pixxxel schubser

    Width/Height return % unit instead px

    willy.sm

      here the code:

       

              photoshop.open(File(sFolderImg + nCont + "D.jpg"));

              var imgD = app.activeDocument;

              photoshop.open(File(sFolderImg + nCont + "R.jpg"));

              var imgR= app.activeDocument;

             

              //alert (imgD.name);

              //alert (imgD.Width.as("px"));

              var imgLottoW=imgD.width.as('px') + imgR.width.as('px');

      imgD.width returs always a 100% and using .as('px') retun NaN:

      I need to get the value in Pixel.

       

      Any idea?

       

      Thanks

        • 1. Re: Width/Height return % unit instead px
          JJMack Most Valuable Participant

          Set Photoshop units.  Ypu may want to add some boiler plate code before you get int your processing code. for example:

          function main() {
            // declare local variables
            var orig_ruler_units = app.preferences.rulerUnits;
            var orig_type_units = app.preferences.typeUnits;
            var orig_display_dialogs = app.displayDialogs;
            app.preferences.rulerUnits = Units.PIXELS; // Set the ruler units to PIXELS
            app.preferences.typeUnits = TypeUnits.POINTS;   // Set Type units to POINTS
            app.displayDialogs = DialogModes.NO; // Set Dialogs off
            try { code(); }
            // display error message if something goes wrong
            catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }
                  // catch(e){$.writeln(e)}  
            app.displayDialogs = orig_display_dialogs; // Reset display dialogs 
            app.preferences.typeUnits  = orig_type_units; // Reset ruler units to original settings 
            app.preferences.rulerUnits = orig_ruler_units; // Reset units to original settings
          }
          

           

           

          You are also using Document canvas as image width  and height Image can be on layers and layers be any size  if thers are areas outside the document canvas the document canvas size is like a clipping mask.  Layer have rectangle bounds.

           

          // 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;
          
          
          try{
            var LB = activeDocument.activeLayer.bounds;
            var LWidth = (LB[2].value) - (LB[0].value);
            var LHeight = (LB[3].value) - (LB[1].value);
            MarkX((LB[0].value + LWidth/2));
            MarkX(LB[0].value);
            MarkX(LB[2].value);
            MarkY((LB[1].value + LHeight/2));
            MarkY(LB[1].value)
            MarkY(LB[3].value)
            alert("'" + activeDocument.activeLayer.name + "' Layer Bounds\nTop Left " +  LB[0].value + "X," +  LB[1].value + "Y   Bottom Right "  +  LB[2].value + "X," +  LB[3].value
            + "Y\nWidth " +  LWidth + "px   Height " + LHeight +"px" 
            + "\nLayer center relative to canvas " + (LB[0].value + LWidth/2) + "X," +  (LB[1].value + LHeight/2) +"Y" 
            );
          }
          catch(e){alert("Requires a layer targered");}
          
          
          // Return the app preferences
          app.preferences.rulerUnits = startRulerUnits;
          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 );
          }
          
          • 2. Re: Width/Height return % unit instead px
            pixxxel schubser MVP & Adobe Community Professional

            Hi willy.sm,

            the preferences.rulerUnits are your friend:

            if(documents.length >1) {
            
            var startRulerUnits = preferences.rulerUnits;
            preferences.rulerUnits = Units.PIXELS;
            //var imgR = documents[0];
            //var imgD = documents[1];
            
            //alert (documents[0].width);
            alert (documents[0].width.as('px'));
            //alert (documents[0].width.as('px'),'px');
            
            //alert (documents[1].width);
            alert (documents[1].width.as('px'));
            //alert (documents[1].width.as('px'),'px');
            
            preferences.rulerUnits = startRulerUnits; // Reset units to original settings  
            }
            

             

            Have fun