Highlighted

Issues accessing histogram.length within active layer in Photoshop CC2015

New Here ,
Oct 08, 2020

Copy link to clipboard

Copied

Hi!

 

I am currently working on a project where I am trying to get the number of pixels per active layer. I have been battling with this one for some days now, but I am not familiar with javascript so I was hoping I could ask for a bit of help.

 

This is a similar problem to another post on how to get the mean value from histogram information (i.e. How do you get Histogram Mean Value?), but the difference is that they want to extract this information form the active document instead of the specific active layer.

 

The issue I am having is that histo.length is not being recognized as an object. There is a related post but they are not trying to access length … (i.e.Get histogram of current layer (not the whole's document) by javascript, CC 2017).

 

Does anyone know of a solution or way around? It would help me a lot!

 

I leave my code below. It is aimed at extracting coordinates (original code I got from another forum), dimensions (length x width) and number of pixels. After defining variables, it loops through layers and later exports it in txt format. I am only having issues with obtaining the number of pixels.

 

Thanks a lot!

Jordi

 

// 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 + ((layerRef.bounds[2]-layerRef.bounds[0])/2);

var y = layerRef.bounds[1].value + ((layerRef.bounds[3]-layerRef.bounds[1])/2);

var length = layerRef.bounds[2]-layerRef.bounds[0];

var width = layerRef.bounds[3]-layerRef.bounds[1];

var coords = "";

var histo = layerRef.histogram;

var histog = histo.length;

var pixels = 0;

for (n = 0; n < histog; n++) {

       pixels = pixels + histog;

    }

 

// Loop to iterate through all layers

 

function recurseLayers(currLayers) {

  for ( var i = 0; i < currLayers.layers.length; i++ ) {

    layerRef = currLayers.layers[i];

    x = layerRef.bounds[0].value;

    y = layerRef.bounds[1].value;

    length = layerRef.bounds[2]-layerRef.bounds[0];

    width = layerRef.bounds[3]-layerRef.bounds[1];

    histo = layerRef.histogram;

    histog = histo.length;

    pixels = 0;

    for (n = 0; n < histog; n++) {

       pixels = pixels + histog;

    }

    coords += layerRef.name + ": " + x + "x" + "," + y + "y" + "," + length + "length" + "," + width + "width" + "," + pixels + "pixels" + "\n";

 

//test if it's a layer set

 

    if ( isLayerSet(currLayers.layers[i]) ) {

      recurseLayers(currLayers.layers[i]);

    }

  }

}

 

//a test for a layer set

 

function isLayerSet(layer) {

  try {

    if ( layer.layers.length > 0 ) {

      return true;

    }

  }

 

  catch(err) {

    return false;

  }

}

 

// Ask the user for the folder to export to

 

var FPath = Folder.selectDialog("Save exported coordinates to");

 

// Detect line feed type

 

if ( $.os.search(/windows/i) !== -1 ) {

  fileLineFeed = "Windows";

}

else {

  fileLineFeed = "Macintosh";

}

 

// Export to txt file

 

function writeFile(info) {

  try {

    var f = new File(FPath + "/" + docName + ".txt");

    f.remove();

    f.open('a');

    f.lineFeed = fileLineFeed;

    f.write(info);

    f.close();

  }

  catch(e){}

}

 

// Run the functions

 

recurseLayers(docRef);

preferences.rulerUnits = defaultRulerUnits;

 

// Set preferences back to user's defaults

 

writeFile(coords);

 

// Show results

 

if ( FPath == null ) {

  alert("Export aborted", "Canceled");

}

else {

  alert("Exported " + layerNum + " layer's coordinates to " + FPath + "/" +

docName + ".txt " + "using " + fileLineFeed + " line feeds.", "Success!");

}

 
 
 
 
TOPICS
Actions and scripting, How to, Mac, Problem or error

Views

32

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

Issues accessing histogram.length within active layer in Photoshop CC2015

New Here ,
Oct 08, 2020

Copy link to clipboard

Copied

Hi!

 

I am currently working on a project where I am trying to get the number of pixels per active layer. I have been battling with this one for some days now, but I am not familiar with javascript so I was hoping I could ask for a bit of help.

 

This is a similar problem to another post on how to get the mean value from histogram information (i.e. How do you get Histogram Mean Value?), but the difference is that they want to extract this information form the active document instead of the specific active layer.

 

The issue I am having is that histo.length is not being recognized as an object. There is a related post but they are not trying to access length … (i.e.Get histogram of current layer (not the whole's document) by javascript, CC 2017).

 

Does anyone know of a solution or way around? It would help me a lot!

 

I leave my code below. It is aimed at extracting coordinates (original code I got from another forum), dimensions (length x width) and number of pixels. After defining variables, it loops through layers and later exports it in txt format. I am only having issues with obtaining the number of pixels.

 

Thanks a lot!

Jordi

 

// 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 + ((layerRef.bounds[2]-layerRef.bounds[0])/2);

var y = layerRef.bounds[1].value + ((layerRef.bounds[3]-layerRef.bounds[1])/2);

var length = layerRef.bounds[2]-layerRef.bounds[0];

var width = layerRef.bounds[3]-layerRef.bounds[1];

var coords = "";

var histo = layerRef.histogram;

var histog = histo.length;

var pixels = 0;

for (n = 0; n < histog; n++) {

       pixels = pixels + histog;

    }

 

// Loop to iterate through all layers

 

function recurseLayers(currLayers) {

  for ( var i = 0; i < currLayers.layers.length; i++ ) {

    layerRef = currLayers.layers[i];

    x = layerRef.bounds[0].value;

    y = layerRef.bounds[1].value;

    length = layerRef.bounds[2]-layerRef.bounds[0];

    width = layerRef.bounds[3]-layerRef.bounds[1];

    histo = layerRef.histogram;

    histog = histo.length;

    pixels = 0;

    for (n = 0; n < histog; n++) {

       pixels = pixels + histog;

    }

    coords += layerRef.name + ": " + x + "x" + "," + y + "y" + "," + length + "length" + "," + width + "width" + "," + pixels + "pixels" + "\n";

 

//test if it's a layer set

 

    if ( isLayerSet(currLayers.layers[i]) ) {

      recurseLayers(currLayers.layers[i]);

    }

  }

}

 

//a test for a layer set

 

function isLayerSet(layer) {

  try {

    if ( layer.layers.length > 0 ) {

      return true;

    }

  }

 

  catch(err) {

    return false;

  }

}

 

// Ask the user for the folder to export to

 

var FPath = Folder.selectDialog("Save exported coordinates to");

 

// Detect line feed type

 

if ( $.os.search(/windows/i) !== -1 ) {

  fileLineFeed = "Windows";

}

else {

  fileLineFeed = "Macintosh";

}

 

// Export to txt file

 

function writeFile(info) {

  try {

    var f = new File(FPath + "/" + docName + ".txt");

    f.remove();

    f.open('a');

    f.lineFeed = fileLineFeed;

    f.write(info);

    f.close();

  }

  catch(e){}

}

 

// Run the functions

 

recurseLayers(docRef);

preferences.rulerUnits = defaultRulerUnits;

 

// Set preferences back to user's defaults

 

writeFile(coords);

 

// Show results

 

if ( FPath == null ) {

  alert("Export aborted", "Canceled");

}

else {

  alert("Exported " + layerNum + " layer's coordinates to " + FPath + "/" +

docName + ".txt " + "using " + fileLineFeed + " line feeds.", "Success!");

}

 
 
 
 
TOPICS
Actions and scripting, How to, Mac, Problem or error

Views

33

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
Oct 08, 2020 0

Have something to add?

Join the conversation