6 Replies Latest reply on Feb 2, 2014 10:32 AM by pixel_a_ted

    Simple JavaScript not working


      I am learning scripting for CS5, just writing some test programs to build up my experience. I'm using Notepad on a Windows 7 machine for the code. I can't seem to figure out what is happening with this script to replicate Photoshop's Fit Image. The problem seems to be in the resizeImage method. If I just create small snipets of code using resizeImage, it works fine. But in this script it doesn't. Thanks for looking.



      // In the first part of the code I open a specified file and output its size. It correctly shows that it's a 640 x 480 image:

      var fileName = prompt("Enter file name.", "c:/image files/fabric1.tif")

      var fileRef = File(fileName)

      var docRef = app.open(fileRef)


      var docHeight = docRef.height

      var docWidth = docRef.width

      alert(docWidth + "  X  " + docHeight)


      // Then I prompt for the largest desired dimension. For illustration purposes, let's say I accept the default 500:

      var size = prompt("Enter size to fit image.", 500)


      // The output shows it's in the first branch of the following if statement and the new width and height values are 500 and 375 as expected.

      if (docWidth >= docHeight)


                alert("In first branch of if statement.")

                docHeightNew = (size/docWidth) * docHeight

                docWidthNew = size

                alert(docWidthNew + "  X  " + docHeightNew)




                alert("In second branch of if statetment.")

                docWidthNew = (size / docHeight) * docWidth

                docHeightNew = size



      // Now I just verify again that the new width and height are 500 and 375 after exiting the if statement, which they are.

      alert(docWidthNew + "  X  " + docHeightNew)


      // Now comes the trouble, with or without the app.activeDocument statement. The image resizes to 1 x 375 pixels:

      docRef = app.activeDocument



      docHeight = docRef.height

      docWidth = docRef.width

      alert(docWidth + "  X  " + docHeight) // image is obviously 1 pixel wide and also output here is 1 x 375.

        • 1. Re: Simple JavaScript not working
          JJMack Most Valuable Participant

          The first thing I would say is your script alert shows 640 px  X 480 px for you Photoshop ruler units are set to pixels.  The script does not set the ruler units to pixels so you might as easily see 100 % X 100 % or 4 in X 3 in be careful when writhing scripts as to what units Photoshop is currently set to use,

          • 2. Re: Simple JavaScript not working
            pixel_a_ted Level 1

            Thank you. Good advice. The ruler units are pixels, but I didn't do anything explicitly in the code to set that. I'm just trying little snippets of code to learn from, rather than trying to do too much at one time. You don't think that's the issue with resizeImage, do you?

            • 3. Re: Simple JavaScript not working
              pixel_a_ted Level 1

              If I just open the image in Photoshop and then run the following, it works:


              docRef = app.activeDocument

              docWidthNew = 500

              docHeightNew = 375


              • 4. Re: Simple JavaScript not working
                JJMack Most Valuable Participant

                does this help

                docRef.resizeImage(docWidthNew + ' px' ,docHeightNew + " px")

                • 5. Re: Simple JavaScript not working
                  JJMack Most Valuable Participant

                  I'm just a hacker and don't really know javascript.  When you get things like width you get a valure and units. In java script you can seem to do math on the retrieved width however when you do you see to loose the units in the result.  You can also just retrieve the value by using width.value. Sometime it seems your can use just values with resize some times you seem to need the units. I don't know the rules.  The 1 may have come from the fist positional parameter not having a correct format for the current rule.

                  • 6. Re: Simple JavaScript not working
                    pixel_a_ted Level 1

                    You the man !!!


                    I used


                         docRef.resizeImage(docWidthNew + ' px',docHeightNew + ' px')


                    as you suggested (I assumed the mixed single and double quotes in your post were a typo) and, voila, the script then worked as expected. Thank you.


                    But that leaves me a bit confused. There was nothing in the Scripting Guide to indicate you needed ' px' in the statement. I think I'm getting the drift of what you are saying about losing the units when I did math on the retrieved width and height.


                    I wonder why the height was coming out as expected, and only the width was wrong. And why the little snipet of code I showed in my third post in this thread worked since I was only send resizeImage pure numbers without any units.


                    Anyhow, is there anywhere where all this might be spelled out a bit more?


                    Thanks again.