Photoshop Javascript Conditional not working

Discussion in 'Javascript' started by bonus, Mar 25, 2010.

  1. bonus

    bonus Guest

    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
    }
    bonus, Mar 25, 2010
    #1
    1. Advertising

  2. 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
    > }
    Jeremy J Starcher, Mar 25, 2010
    #2
    1. Advertising

  3. bonus <> 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'
    Lasse Reichstein Nielsen, Mar 25, 2010
    #3
  4. bonus

    bonus Guest

    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, Mar 25, 2010
    #4
  5. bonus

    bonus Guest

    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!
    bonus, Mar 25, 2010
    #5
  6. 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.
    Jeremy J Starcher, Mar 25, 2010
    #6
  7. bonus

    bonus Guest

    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
    }
    bonus, Mar 25, 2010
    #7
  8. 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
    }
    Jeremy J Starcher, Mar 25, 2010
    #8
  9. bonus

    TK Guest

    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);}
    TK, Mar 26, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1.  opalinski from opalpaweb
    Replies:
    9
    Views:
    2,639
    opalinski from opalpaweb
    Mar 1, 2006
  2. Keith Blake

    photoshop website templates

    Keith Blake, Jan 8, 2004, in forum: HTML
    Replies:
    4
    Views:
    382
    Whitecrest
    Jan 8, 2004
  3. Jesse
    Replies:
    2
    Views:
    443
    Jesse
    May 27, 2004
  4. Replies:
    0
    Views:
    125
  5. slebetman

    Javascript in Adobe Photoshop

    slebetman, Jan 6, 2010, in forum: Javascript
    Replies:
    2
    Views:
    88
    Thomas 'PointedEars' Lahn
    Jan 6, 2010
Loading...

Share This Page