Width/Height return % unit instead px

New Here ,
May 02, 2017 May 02, 2017

Copy link to clipboard

Copied

here the code:

        photoshop.open(File(sFolderImg + nCont + "D.jpg"));

        var imgD = app.activeDocument;

        photoshop.open(File(sFolderImg + nCont + "R.jpg"));

        var imgR= app.activeDocument;

       

        //alert (imgD.name);

        //alert (imgD.Width.as("px"));

        var imgLottoW=imgD.width.as('px') + imgR.width.as('px');

imgD.width returs always a 100% and using .as('px') retun NaN:

I need to get the value in Pixel.

Any idea?

Thanks

TOPICS
Actions and scripting

Views

1.3K

Likes

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
LEGEND ,
May 02, 2017 May 02, 2017

Copy link to clipboard

Copied

Set Photoshop units.  Ypu may want to add some boiler plate code before you get int your processing code. for example:

function main() {

  // declare local variables

  var orig_ruler_units = app.preferences.rulerUnits;

  var orig_type_units = app.preferences.typeUnits;

  var orig_display_dialogs = app.displayDialogs;

  app.preferences.rulerUnits = Units.PIXELS; // Set the ruler units to PIXELS

  app.preferences.typeUnits = TypeUnits.POINTS;   // Set Type units to POINTS

  app.displayDialogs = DialogModes.NO; // Set Dialogs off

  try { code(); }

  // display error message if something goes wrong

  catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }

        // catch(e){$.writeln(e)} 

  app.displayDialogs = orig_display_dialogs; // Reset display dialogs

  app.preferences.typeUnits  = orig_type_units; // Reset ruler units to original settings

  app.preferences.rulerUnits = orig_ruler_units; // Reset units to original settings

}

You are also using Document canvas as image width  and height Image can be on layers and layers be any size  if thers are areas outside the document canvas the document canvas size is like a clipping mask.  Layer have rectangle bounds.

// enable double-clicking from Mac Finder or Windows Explorer

#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events

app.bringToFront();

// Save the current preferences

var startRulerUnits = app.preferences.rulerUnits;

// Set Photoshop to use pixels

app.preferences.rulerUnits = Units.PIXELS;

try{

  var LB = activeDocument.activeLayer.bounds;

  var LWidth = (LB[2].value) - (LB[0].value);

  var LHeight = (LB[3].value) - (LB[1].value);

  MarkX((LB[0].value + LWidth/2));

  MarkX(LB[0].value);

  MarkX(LB[2].value);

  MarkY((LB[1].value + LHeight/2));

  MarkY(LB[1].value)

  MarkY(LB[3].value)

  alert("'" + activeDocument.activeLayer.name + "' Layer Bounds\nTop Left " +  LB[0].value + "X," +  LB[1].value + "Y   Bottom Right "  +  LB[2].value + "X," +  LB[3].value

  + "Y\nWidth " +  LWidth + "px   Height " + LHeight +"px"

  + "\nLayer center relative to canvas " + (LB[0].value + LWidth/2) + "X," +  (LB[1].value + LHeight/2) +"Y"

  );

}

catch(e){alert("Requires a layer targered");}

// Return the app preferences

app.preferences.rulerUnits = startRulerUnits;

function MarkX(x) {

// =======================================================

var idMk = charIDToTypeID( "Mk  " );

    var desc61 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

        var desc62 = new ActionDescriptor();

        var idPstn = charIDToTypeID( "Pstn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc62.putUnitDouble( idPstn, idPxl, x);

        var idOrnt = charIDToTypeID( "Ornt" );

        var idOrnt = charIDToTypeID( "Ornt" );

        var idVrtc = charIDToTypeID( "Vrtc" );

        desc62.putEnumerated( idOrnt, idOrnt, idVrtc );

    var idGd = charIDToTypeID( "Gd  " );

    desc61.putObject( idNw, idGd, desc62 );

executeAction( idMk, desc61, DialogModes.NO );

}

function MarkY(y) {

// =======================================================

var idMk = charIDToTypeID( "Mk  " );

    var desc63 = new ActionDescriptor();

    var idNw = charIDToTypeID( "Nw  " );

        var desc64 = new ActionDescriptor();

        var idPstn = charIDToTypeID( "Pstn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc64.putUnitDouble( idPstn, idPxl, y );

        var idOrnt = charIDToTypeID( "Ornt" );

        var idOrnt = charIDToTypeID( "Ornt" );

        var idHrzn = charIDToTypeID( "Hrzn" );

        desc64.putEnumerated( idOrnt, idOrnt, idHrzn );

    var idGd = charIDToTypeID( "Gd  " );

    desc63.putObject( idNw, idGd, desc64 );

executeAction( idMk, desc63, DialogModes.NO );

}

JJMack

Likes

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
New Here ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

Sorry but i got the similar problem when trying to get width and height of active document. And change Unit to Pixel doesn't work

I just open 1 png image into photoshop then run the script. Alert width and height and got "NaN px"

Here is my simple code:

 

var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

try{
    var doc = app.activeDocument;
    var currentWidth = doc.width;
    var currentHeight = doc.height;
    alert(currentWidth);
    alert(currentHeight);
    var lyrBound = doc.activeLayer.bounds;
    var LWidth = (lyrBound[2].value) - (lyrBound[0].value);
    var LHeight = (lyrBound[3].value) - (lyrBound[1].value);
    alert(LWidth);
    alert(LHeight);
}catch(e){
    alert(e);
}
app.preferences.rulerUnits = startRulerUnits;
 
I tried this script with 2 png image file: "spark_1" and "spark_1_resize"
 - "spark_1": 1024x1024 px => script showing "NaN px" with it
- "spark_1_resize": 512x512 px => script works and showing "512px"
 
I have no idea why is that
 
I really appreciate with any help ❤
Thank you

Likes

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 Community Professional ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

@Vũ Ngọc22029655edlq 

 

spark_1.png has a resolution of 0 ppi (zero)

 

spark_1_resize.png has a resolution of 1 ppi (one)

 

If you use image size with resampling turned off and assign any PPI value, then spark_1.png will no longer error with "not a number".

 

The question is, how did you create an image with 0 ppi? Photoshop will not let you do that, so it must have come from some other software or had metadata resolution changed to zero.

Likes

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
New Here ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

LATEST

❤❤❤

Thank you. You saved my life
I know there is different between these images but i have no idea what it is

Likes

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 Community Professional ,
May 02, 2017 May 02, 2017

Copy link to clipboard

Copied

Hi willy.sm,

the preferences.rulerUnits are your friend:

if(documents.length >1) {

var startRulerUnits = preferences.rulerUnits;

preferences.rulerUnits = Units.PIXELS;

//var imgR = documents[0];

//var imgD = documents[1];

//alert (documents[0].width);

alert (documents[0].width.as('px'));

//alert (documents[0].width.as('px'),'px');

//alert (documents[1].width);

alert (documents[1].width.as('px'));

//alert (documents[1].width.as('px'),'px');

preferences.rulerUnits = startRulerUnits; // Reset units to original settings 

}

Have fun

Likes

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