3 Replies Latest reply on Jan 27, 2013 11:54 AM by Michael L Hale

    javascript using ScriptUI for resizing images

    mrfsrf5

      Hi all,

      this is my first post here and i must say that im a newbie in javascript.

      Anyway, Im trying to create a script that will open dialog box where a user can enter dimensions (in pixels and using only one "edittext") and by storing that number in variable for resizing.

      Here is my code. Problem is that no matter what number am i entering into the field, photoshop resizes the image to 1px width to 1 px height.

      Any thoughts why?

       

       

       

      app.displayDialogs = DialogModes.NO;

      app.preferences.rulerUnits = Units.PIXELS;

       

       

      var dlg = new Window('dialog', 'Alert Box Builder',[100,100,480,245]);

      dlg.msgPnl = dlg.add('panel', [25,15,355,130], 'Messages');

      dlg.msgPnl.titleSt = dlg.msgPnl.add('statictext', [15,15,105,35], 'Alert box title:'); 
dlg.msgPnl.titleEt = dlg.msgPnl.add('edittext', [115,15,315,35], '500');

      dlg.buttonRef = dlg.msgPnl.add('button',  [15, 70, 140, 95] , 'OK', { name:'ok'});

       

      dlg.msgPnl.titleEt.onChange = function(){

         

          var RefDimension = this.text;   

          app.activeDocument.resizeImage(RefDimension,undefined,undefined,undefined); //RESIZE IMAGE TO FINAL DIMENSIONS

          alert(RefDimension); // this is only for checkup to see if the photshop is parsing the correct number, which is fine.

       

      }

       

       

      dlg.show();

        • 1. Re: javascript using ScriptUI for resizing images
          Paul Riggott Level 6

          This might help...

           

           

          app.displayDialogs = DialogModes.NO;
          app.preferences.rulerUnits = Units.PIXELS;
          
          
          var dlg = new Window('dialog', 'Alert Box Builder',[100,100,480,245]); 
          dlg.msgPnl = dlg.add('panel', [25,15,355,130], 'Messages'); 
          dlg.msgPnl.titleSt = dlg.msgPnl.add('statictext', [15,15,105,35], 'Alert box title:');
          dlg.msgPnl.titleEt = dlg.msgPnl.add('edittext', [115,15,315,35], '500'); 
          dlg.buttonRef = dlg.msgPnl.add('button',  [15, 70, 140, 95] , 'OK', { name:'ok'});
          
          dlg.buttonRef.onClick = function(){
              dlg.close(0);
              var RefDimension = Number(dlg.msgPnl.titleEt.text);    
              app.activeDocument.resizeImage(RefDimension,undefined,undefined,undefined); //RESIZE IMAGE TO FINAL DIMENSIONS
              alert(RefDimension); // this is only for checkup to see if the photshop is parsing the correct number, which is fine.
          }
          
          
          dlg.show();
          
          
          • 2. Re: javascript using ScriptUI for resizing images
            mrfsrf5 Level 1

            thanks a bunch

            that worked just fine.

            Somewhere in my mind I thought that the problem might lie in a variable type..that the photoshop somehow is interpreting variable as a string instead of  number

            • 3. Re: javascript using ScriptUI for resizing images
              Michael L Hale Level 5

              Somewhere in my mind I thought that the problem might lie in a variable type..that the photoshop somehow is interpreting variable as a string instead of  number

              Yes, an edittext control text property will always be a string even if the string contains digits. Paul's example of using Number() to explicitly convert the string to a number will work as long as the user only enters digits into the edittext. So you should either check that the conversion resulted in a valid number or use an event handler on the edittext control to limit input to digits only.

               

              Also resizeImage expects UnitValue object for width and height. You can supply a Number object but Photoshop will convert that to a UnitValue using the current ruler unit. So you either need to set the ruler unit before hand to match the desired unit of the edittext as Paul did in his example. Or do the convert to UnitValue yourself using which ever unit you need.

               

              It is also common practice to restore the rulerUnit and displayDialogs to the original settings if your script does change them.