• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Can't subtract one var from another in js

Explorer ,
Apr 26, 2012 Apr 26, 2012

Copy link to clipboard

Copied

I have a script which I'm modifying, it resizes an image, adds a black border then adds white borders to replicate the look of a 10x8 inch print.

This line is giving me trouble and I can't seee why!

     docRef.resizeCanvas ( docRef.width + (portraitPrintW - docRef.width), docRef.height, AnchorPosition.MIDDLECENTER);



I've already set

     var docRef = activeDocument; var portraitPrintW = 576;

The script has resized the image to 72ppi and to fit 676ppi long side. Its then added a 2px black border all around. At this point the image should be 454px wide. I want to add a white border to the left and right to give a total width of 576. So the above line should be 454 + (576-454) which is 454+126=576 therefore I'm doing a Resize Canvas 576, 680 (the current height) middle centre (with a white background - set as a var earlier).

What actually happens is the canvas is resized to be 334 wide (height is correct) - how did that happen?

Replacing a couple or vars with real number allows the script to work as expected

     docRef.resizeCanvas ( (docRef.width + (576 - 450) ), docRef.height, AnchorPosition.MIDDLECENTER);

and

     docRef.resizeCanvas ( (docRef.width + (portraitPrintW - 450) ), docRef.height, AnchorPosition.MIDDLECENTER);

also works as expected

Maybe I can't use the same var twice in a calculation, no because even this doesn't work

     var currentWidth = docRef.width;

     docRef.resizeCanvas ( (docRef.width + (portraitPrintW - currentWidth) ), docRef.height, AnchorPosition.MIDDLECENTER);

So why does ( docRef.width + (portraitPrintW - docRef.width) return 334, not 676?

    

Thanks for any clues.

TOPICS
Actions and scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Explorer ,
Apr 26, 2012 Apr 26, 2012

Copy link to clipboard

Copied

There's a typo in my maths,  454+126=576 should read  454+122=576. So that's not the problem!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2012 Apr 26, 2012

Copy link to clipboard

Copied

Huh?

docRef.width + (portraitPrintW - docRef.width) = portraitPrintW

So what are you trying to achieve?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2012 Apr 26, 2012

Copy link to clipboard

Copied

I'm trying to take an image which is 454px wide and use resize canvas to make it 576px wide. So that calculation makes sence to me, but if there's a better way!

Er, or are you saying why don't I just use portraitPrintW as that will always be the answer? That might be a better way but it still doesn't explain why the calclation returns 334, not (as you say, portraitPrintW) 576.

Thanks, its a helpful answer as its making me look from a different angle.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 26, 2012 Apr 26, 2012

Copy link to clipboard

Copied

I think that part of the problem is that you are adding UnitValue objects and Number objects.

One fix would be

var docRef = activeDocument; var portraitPrintW = New UnitValue(576,'px');

Another way would to use

docRef.resizeCanvas ( new UnitValue(docRef.width.as('px') + (576 - 450) ),'px'), docRef.height, AnchorPosition.MIDDLECENTER);

Or set your ruler to pixels and just use Numbers.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2012 Apr 27, 2012

Copy link to clipboard

Copied

I think I'm already setting the ruler units to pixels, here I'm saving them to a var in order to reset them after the scrip is finished.

        var strtRulerUnits = preferences.rulerUnits;

        var strtTypeUnits = preferences.typeUnits;

        preferences.rulerUnits = Units.PIXELS;

        preferences.typeUnits = TypeUnits.PIXELS;

and that appears to be working. The calculation returns the expected answer when using numeric values in place of some vars, as I say:

     docRef.resizeCanvas ( docRef.width + (portraitPrintW - docRef.width), docRef.height, AnchorPosition.MIDDLECENTER);

returns a different answer from

      docRef.resizeCanvas ( (docRef.width + (576 - 450) ), docRef.height, AnchorPosition.MIDDLECENTER);

even though var portraitPrintW is set to 576 and docRef.width (should) be 450.

Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 12, 2012 May 12, 2012

Copy link to clipboard

Copied

LATEST

When you get values such as docRef.width, it adds the unit values ie: px.  Sometimes it's best to strip out all the unit values and just work with numbers, so use parseFloat() or parseInt().

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines