Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Photoshop Javascript Conditional not working (http://www.velocityreviews.com/forums/t940719-photoshop-javascript-conditional-not-working.html)

bonus 03-25-2010 05:29 PM

Photoshop Javascript Conditional not working
 
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
}

Jeremy J Starcher 03-25-2010 05:39 PM

Re: Photoshop Javascript Conditional not working
 
On Thu, 25 Mar 2010 10:29:47 -0700, bonus wrote:

> 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




>
>
>
> #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
> }



Lasse Reichstein Nielsen 03-25-2010 05:40 PM

Re: Photoshop Javascript Conditional not working
 
bonus <tkphoto@comcast.net> writes:

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
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'


bonus 03-25-2010 06:46 PM

Re: Photoshop Javascript Conditional not working
 
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
}


bonus 03-25-2010 06:51 PM

Re: Photoshop Javascript Conditional not working
 
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.

>
> > * * * if (doc.width > doc.height) {

>


Thanks!


Jeremy J Starcher 03-25-2010 06:52 PM

Re: Photoshop Javascript Conditional not working
 


On Thu, 25 Mar 2010 11:46:42 -0700, bonus wrote:

> 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.

bonus 03-25-2010 07:03 PM

Re: Photoshop Javascript Conditional not working
 
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
}

Jeremy J Starcher 03-25-2010 07:18 PM

Re: Photoshop Javascript Conditional not working
 
On Thu, 25 Mar 2010 12:03:00 -0700, bonus wrote:

> 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:
>> But, if you convert to an integer like this
>> parseInt('900px', 10) > 1200 == false


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
}



TK 03-26-2010 11:12 PM

Re: Photoshop Javascript Conditional not working
 
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);}


All times are GMT. The time now is 06:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.