E
E
Egor Chistyakov2015-09-16 07:29:23
JavaScript
Egor Chistyakov, 2015-09-16 07:29:23

How to automate canvas size rounding in Photoshop?

In my line of work, I often have to process heaps of files, performing heaps of operations, all of which are beautifully templated. Except for one - rounding the size of the type 5000.5x1500.5mm down .
For this I wrote a simple script:

app.preferences.rulerUnits = Units.MM;
app.activeDocument.resizeCanvas (Math.floor(app.activeDocument.width), Math.floor(app.activeDocument.height), AnchorPosition.TOPLEFT);

But, unfortunately, when one of the dimensions is ALREADY quite round, the script sometimes subtracts a millimeter - and 1500, for example, turns into 1499. I tried to do something like , but, you see, I don’t know JS well. What am I doing wrong? Math.floor(app.activeDocument.width.toFixed(2))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Karabanov, 2015-09-16
@chegr

It's not about knowing JS, but about understanding what is happening.
The size must be calculated in advance and checked, I believe, whether it is divisible by 2 without a remainder. If it is divisible, then nothing needs to be changed;
Before app.activeDocument.resizeCanvas, you need to add an if statement in which to check whether rounding is necessary. In the app.activeDocument.resizeCanvas method itself, you need to substitute variables containing the rounding/non-rounding result that passed the if test, and not calculate the result on the spot.

app.preferences.rulerUnits = Units.MM;
width  = app.activeDocument.width;
height = app.activeDocument.height;
// Не знаю синтаксис, пишу по наитию, но должно выглядеть как-то так
if(width % 2)  width  = Math.floor(width);
if(height % 2) height = Math.floor(height);

app.activeDocument.resizeCanvas (width, height, AnchorPosition.TOPLEFT);

M
Misha Kogan, 2015-09-16
@Kogan4ik

Try the Math.ceil();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question