Photoshop: Subtraction in script gives wrong result

  • 1
  • Problem
  • Updated 2 years ago
  • (Edited)
Hello,

I'm trying to run a simple script in photoshop that does a simple subtraction operation. But for a very strange reason the subtraction gives the wrong result.

Here is the script I'm trying to run:
// Enables double-click launching from the Mac Finder or Windows Explorer
#target photoshop

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var layerNum = app.activeDocument.artLayers.length;

// Define variable for the active layer in the active document
var layerRef = app.activeDocument.activeLayer;

// Define varibles for x and y of layers
var x = layerRef.bounds[0].value;
var y = layerRef.bounds[1].value;
var coords = "";

var designResolutionWidth = 2133;
var designResolutionHeight = 1600;

var resolutionWidthDelta = (docRef.width - designResolutionWidth) * 0.5;
x = 1423;
sum = x - resolutionWidthDelta ;

coords += "x = " + x + "\n";
coords += "resolutionWidthDelta = " + resolutionWidthDelta + "\n";
coords += "sum = " + sum + "\n";

alert(coords);

Just save it in a jsx file in your scripts folder, create a new document and run it. See what the alert shows you.
For me the result shown in the alert for the sum is wrong and I have no idea why.
Anyone encountered something like this? Am I missing something?

Thanks
Photo of Udi Rubinstein

Udi Rubinstein

  • 2 Posts
  • 0 Reply Likes
  • frustrated

Posted 3 years ago

  • 1
Photo of Udi Rubinstein

Udi Rubinstein

  • 2 Posts
  • 0 Reply Likes
A few more details:
1. I'm running Photoshop CC 2015 on MacOS X 10.10.3.
2. I'm creating a new document with a size of 1024x1024 pixels.
Photo of Pedro Marques

Pedro Marques

  • 159 Posts
  • 25 Reply Likes
You are working with UnitValue() using document.width. You need its value only, not the units.

Try change docRef.width to docRef.width.value:

var resolutionWidthDelta = (docRef.width.value - designResolutionWidth) * 0.5;