Skip to main content
Inspiring
March 6, 2020
Answered

Javascript to test an image size and change size if needed

  • March 6, 2020
  • 2 replies
  • 1454 views

I'm trying write a script to test an open image to see if its below 1000px and if so res it up to 1000px, and also test if its above 10,000px and res it down to 10,000px. I think I would set this up as and else if statement but this does'nt work any ideas where I'm going wrong?

 

if(app.activeDocument.width.value<1000){e
  app.activeDocument.resizeImage(
                  new UnitValue(1000, "px"),
                  currentHeight,
                  currentResolution,
                  ResampleMethod.BILINEAR);
	}else if (app.activeDocument.width.value>10000){
   app.activeDocument.resizeImage(              
                  new UnitValue(10000, "px"),
                  currentHeight,
                  currentResolution,
                  ResampleMethod.BILINEAR);
  }else {}

 

This topic has been closed for replies.
Correct answer r-bin
Have you set before this code
app.preferences.rulerUnits = Units.PIXELS;
 

2 replies

r-binCorrect answer
Brainiac
March 6, 2020
Have you set before this code
app.preferences.rulerUnits = Units.PIXELS;
 
Inspiring
March 6, 2020

Awesome, that was it. Thank you!

 

 

Kukurykus
Brainiac
March 6, 2020

If you keep currentHeight and currentResolution in variables, just remove e (at end of first line).

Inspiring
March 6, 2020

When I run this it does not work as expected. If its under 1000 it scales everything to 1000 including the spreads that are around 1200px, if its over it just doesn't do anything. Not sure whats going on here. Do I need to be specific about the pixels something like <1000px

 

These are the variables.

 

var currentHeight = null;
var currentResolution = 200;

Kukurykus
Brainiac
March 6, 2020

What is spreads?