@magnos23200120
Try this script, as it uses ExtendScript Action Manager code to retrieve the artboard bounds, it may not work on all versions of Photoshop. I have tested in v2021 and v2024:
#target photoshop
try {
// Checks that the active layer is a group/artboard/frame
if (app.activeDocument.activeLayer.typename === "LayerSet") {
alert("Select a layer inside the artboard and run the script again...")
} else {
// Set the ruler units to px, don't assume that they are set to px
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Set the original layer as a variable
var theLayer = app.activeDocument.activeLayer;
// Select the parent of the active layer, which is *assumed* to be an artboard
app.activeDocument.activeLayer = app.activeDocument.activeLayer.parent;
try {
// Active Artboard Dimensions, by Rune L-H
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var artBoardRect = executeActionGet(ref).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
var artBoardRectWidth = new UnitValue(artBoardRect.getDouble(stringIDToTypeID("right")) - artBoardRect.getDouble(stringIDToTypeID("left")), "px");
var artBoardRectHeight = new UnitValue(artBoardRect.getDouble(stringIDToTypeID("bottom")) - artBoardRect.getDouble(stringIDToTypeID("top")), "px");
} catch (e) {
alert("The problem could be that the layer is inside of a group..." + "\r" + e + ' ' + e.line);
// Return to the original layer
app.activeDocument.activeLayer = theLayer;
}
// Get the active layer
var layer = app.activeDocument.activeLayer;
// Gets the layer dimensions
var layerWidth = theLayer.bounds[2].value - theLayer.bounds[0].value;
var layerHeight = theLayer.bounds[3].value - theLayer.bounds[1].value;
// Calculates the total area of the screen/artboard
var artboardArea = artBoardRectWidth.value * artBoardRectHeight.value;
// Calculate the layer area
var layerArea = layerWidth * layerHeight;
// Calculate the total percentage of the layer
var totalPercentage = (layerArea / artboardArea) * 100;
// Checks if the layer occupies more than 30% of the screen/artboard
if (totalPercentage >= 30) {
alert("The layer occupies **" + totalPercentage.toFixed(2) + "%** of the screen/artboard. Test passed!");
} else {
alert("The layer occupies **" + totalPercentage.toFixed(2) + "%** of the canvas/artboard. Increase the layer to pass the test!");
}
// Return to the original layer
app.activeDocument.activeLayer = theLayer;
// Return the original ruler units
app.preferences.rulerUnits = originalRulerUnits;
}
} catch (e) {
alert("Error!" + "\r" + e + ' ' + e.line);
}
P.S. I'll ask a moderator to change this topic from "bugs" to "discussion", bugs are for Adobe bugs, not for problems with code created by generative AI platforms.
... View more