Skip to main content
yajiviki
Known Participant
June 22, 2021
Answered

How do we get the Pixel/Centimeter information ImageSize window [through Javascript]

  • June 22, 2021
  • 2 replies
  • 3384 views

Hi Everyone!,

Is this possible to get Pixel/Centimeter information ImageSize window through JavaScript?

Thank in Advance

 

 

This topic has been closed for replies.
Correct answer r-bin

You can do it like this

 

 

//activeDocument.save(); // must be saved to force update metadata 

var xmp = new XML(activeDocument.xmpMetadata.rawData);  
var s = String(eval("xmp.*::RDF.*::Description.*::ResolutionUnit"));  

if (s == "3") alert("cm")
if (s == "2") alert("inch")

 

upd. I don't seem to understand again. I am groot .... : )

 

2 replies

r-binCorrect answer
Braniac
June 22, 2021

You can do it like this

 

 

//activeDocument.save(); // must be saved to force update metadata 

var xmp = new XML(activeDocument.xmpMetadata.rawData);  
var s = String(eval("xmp.*::RDF.*::Description.*::ResolutionUnit"));  

if (s == "3") alert("cm")
if (s == "2") alert("inch")

 

upd. I don't seem to understand again. I am groot .... : )

 

yajiviki
yajivikiAuthor
Known Participant
June 22, 2021

Hi R-bin,

Thank you for the prompt response. It's working like a charm.

Is this possible to change Pixel/Centimeter to Pixel/Inch using java script?

 

-yajiv

 

Stephen Marsh
Braniac
June 22, 2021

I'm not r-bin, so I'm not sure if this is out of context or if I am misunderstanding... Multiplication is the opposite of division. So swap / 2.54 for * 2.54

Kukurykus
Braniac
June 22, 2021
activeDocument.resolution / 2.54
Stephen Marsh
Braniac
June 22, 2021

Curious minds need to know...

 

Photoshop image size truncates the display to 3 decimal places for a 240 PPI doc:

 

From: 94.488188976378 PPCM

To: 94.488 PPCM

 

 

var docResCM = app.activeDocument.resolution / 2.54;
var docResFixed = docResCM.toFixed(3);
var docResRounded = Math.round(docResCM * 1000) / 1000;
alert(docResCM + "\r" + docResFixed + "\r" + docResRounded);

 

 

The code above results in 94.488

 

However - Math.trunc(n) is not supported if I understand things correctly to easily chop off after (3).

 

Surely there must be a better way to truncate to 3 places than to transform the number into a string, regex remove all digits after the dot + 3 places, and then possibly return the string to a number if the end use requires a number for further calculations?

 

P.S. In the screenshot above, the truncation is to 2 decimal places, however, mine truncates to 3?

118.110236220472441

 

Kukurykus
Braniac
June 22, 2021