Photoshop Javascript Conditional not working

B

bonus

Any users out there use Javascript for Scripting Photoshop? My
conditional is not working.

Thanks in Advance!



#target photoshop
app.bringToFront();


function fitImage(newImgSize,res) {
var doc = app.activeDocument;
if (res == undefined) res = undefined;
if (doc.width > doc.height) {
doc.resizeImage(new UnitValue(newImgSize,'px'), undefined,
res, ResampleMethod.BICUBIC);
} else
if (doc.width < doc.height) {
doc.resizeImage(undefined, new UnitValue(newImgSize,'px'),
res, ResampleMethod.BICUBIC);
} else
if (doc.width == doc.height) {
doc.resizeImage(new UnitValue(newImgSize,'px'), new
UnitValue(newImgSize,'px'), res, ResampleMethod.BICUBIC);
}
};


var doc = app.activeDocument;

if (doc.height > '1200px') {
fitImage (1800,300) //pixels - resolution
}
if (doc.width > '1200px') {
fitImage (1800,300) //pixels - resolution
}
else {
fitImage (900,300) //pixels - resolution
}
 
J

Jeremy J Starcher

Any users out there use Javascript for Scripting Photoshop? My
conditional is not working.

Thanks in Advance!

I don't use the Photoshop scripting. However, had you let us know
/which/ conditional was not working, we might be able to give some help
anyways.

Reaching into my mind-reading powers, I am going to take a while guess
that your trouble is here:
if (doc.width > '1200px') {

if (typeof doc.width === 'string') then in lexiconal sorting
'900px' > '1200px' == true

But, if you convert to an integer like this
parseInt('900px', 10) > 1200 == false
 
L

Lasse Reichstein Nielsen

You need to write better error descriptions :)
In this case, what should the code do, and what does it actually do
(i.e., what do you expect, and what actually happens).
Since we don't know the intention of the code, it's at best a guess
at what part is not doing what it should.

That said:
function fitImage(newImgSize,res) {
var doc = app.activeDocument;
if (res == undefined) res = undefined;

Seems wasteful to assign undefined to res just after having checked
that it is already undefined (or null).
if (doc.width > doc.height) {

The indentation here suggests that it should be part of a branch
from the if above. However, it isn't. This might be your problem.

var doc = app.activeDocument;

if (doc.height > '1200px') {

Doing string compare is unlikely to be what you want, since it is
lexical comparison: "800px" > "1200px".
Perhaps instead use:
if (parseInt(doc.height, 10) > 1200) {

/L
 
B

bonus

Yes I admit, I was a little distract while sending the original post.

Let me clarify my goals:

To look at the pixel range within the image and see if the image is
over 1200 pixels to resize the image to 1800 pixels.
If the pixel range is under 1200 pixels then resize the image to 900
pixels.

I believe the problem may be in the following area, but I attached the
whole script so you can see the function.

if (doc.height > '1200px') {
fitImage (1800,300) //pixels - resolution
}

if (doc.width > '1200px') {
fitImage (1800,300) //pixels - resolution
}

else {
fitImage (900,300) //pixels - resolution
}
 
B

bonus

Thank you,

I think you may have hit a good point with your advise.


By the way the "undefined section" of my code is the script which
compares the longest side of the image. I didn't paste this area of
the code because it isn't fully written out, but it will determine
which side needs to have canvas added to square the image off.

Thanks!
 
J

Jeremy J Starcher

I believe the problem may be in the following area, but I attached the
whole script so you can see the function.

if (doc.height > '1200px') {

Both Lasse Reichstein and I address that very issue in our previous posts.
 
B

bonus

Unfortunately I am receiving the same results. When the images
process, regardless of size, the images are sizing to 900 pixels.



var doc = app.activeDocument;

if (parseInt(doc.height, 10) > '1200px')
{
fitImage (1800,300) //pixels - resolution
}

if (parseInt(doc.height, 10) > '1200px')
{
fitImage (1800,300) //pixels - resolution
}

else
{
fitImage (900,300) //pixels - resolution
}
 
J

Jeremy J Starcher

Unfortunately I am receiving the same results. When the images process,
regardless of size, the images are sizing to 900 pixels.

Works as coded then. *Grin*
if (parseInt(doc.height, 10) > '1200px') {

Now you are comparing an integer (the result of "parseInt(doc.height,
10)") against a string value '1200px'.

Look at the example I gave you:
You'll note I compare integer against integer.

Your other code stood a chance of resizing the image twice, once for the
height and the other time for the width.

I would write your code like this:

var doc = app.activeDocument;

if (
(parseInt(doc.height, 10) > 1200) ||
(parseInt(doc.width, 10) > 1200)
)
{
fitImage (1800,300) //pixels - resolution
}
else
{
fitImage (900,300) //pixels - resolution
}
 
T

TK

Just an FYI, I have the solution now.

Thanks for all the help!


#target photoshop
app.bringToFront();

function fitImage(newImgSize,res) {
var doc = app.activeDocument;
if (doc.width > doc.height) {doc.resizeImage(new
UnitValue(newImgSize,'px'), undefined, res, ResampleMethod.BICUBIC);}
else if (doc.width < doc.height) {doc.resizeImage(undefined, new
UnitValue(newImgSize,'px'), res, ResampleMethod.BICUBIC);}
else (doc.width == doc.height) {doc.resizeImage(new
UnitValue(newImgSize,'px'), new UnitValue(newImgSize,'px'), res,
ResampleMethod.BICUBIC);}
}

var doc = app.activeDocument;

if (doc.height.as("px") > 1200){fitImage (1800,300);}
else if (doc.width.as("px") > 1200){fitImage (1800,300);}
else {fitImage (900,300);}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top