UnitValue parameter type

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

pixel_a_ted

UnitValue parameter type

Post by pixel_a_ted »

I'm a beginner at using javascript with Photoshop CS5. I ran into an issue using the resizeImage method of Document. If I do something like the following, it appears to work as expected:

docRef = app.activeDocument
docWidthNew = 500
docHeightNew = 375
docRef.resizeImage(docWidthNew,docHeightNew)

I then tried a slightly more complicated script to get the width and height from the image (which worked based on an alert statement used) and do some simple math on those values. Then when I used resizeImage as in the last step above, I got unexpected results.

I posted on the Adobe scripting forum and someone told me to try the following instead, and indeed it worked. (I should add that ruler units were set to pixels in Photoshop preferences throughout this testing).

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

My followup questions were not answered so I am glad to have found this forum.

Basically, I don't recall seeing anything in the Scripting Guide to indicate you needed to add 'px' in the statement, so I never would have done this. Can someone explain what's going on here or provide a reference that explains how "UnitValue" parameters work, such as are used for width and height in the resizeImage method? Do they have two parts to them, a value and a unit? If so, why does the snippet of code at the beginning of my post work?

Thank you.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

UnitValue parameter type

Post by pfaffenbichler »

When you refer to ExtendScript Toolkit’s Photoshop Object Model Viewer and search for »resizeImage« you get (partially)
Document.resizeImage (width: UnitValue , height: UnitValue , resolution: number , resampleMethod: ResampleMethod )
so obviously UnitValues are required.

A unitValue can for example be set up like
Code: Select allvar someValue = new UnitValue (50, "px")
For more on UnitValues check out »UnitValue« under »Core JavaScript Classes« in the Object Model Viewer.
pixel_a_ted

UnitValue parameter type

Post by pixel_a_ted »

Thanks. I'm having problems getting ExtendScript to work (I started a separate thread) so some of this information may not be available to me at this time. I am wondering how to do math on UnitValues if they have both a value and a unit.

So if I want to reduce the height of an image by, say, a factor of two and I obtain the height using the following:

var docHeight = docRef.height

and I do something like this:

docHeightNew = docHeight/2

then does docHeightNew still retain the units that docHeight has? That didn't seem to be the case in the other script I was playing with, as I had to explicit add the 'px' in resizeImage. Also, why does something like this work:

docRef = app.activeDocument
docWidthNew = 500
docHeightNew = 375
docRef.resizeImage(docWidthNew,docHeightNew)

if 'px' is not explicitly used?

Thanks.
pfaffenbichler

UnitValue parameter type

Post by pfaffenbichler »

I think UnitValues can be multiplied/divided and retain their type.

if 'px' is not explicitly used?
As far as I can tell the rulerUnits setting is used if no type is defined.
So if your rulerUnits are not px but mm the document is sized in mm.

One possible practice to avoid such issues is setting the rulerUnits to pixels and the resolution to 72 in any Script and resetting those values t other original states at the end.