@magnos23200120
As the script uses ExtendScript Action Manager code to retrieve the artboard bounds and to check if the layer is an artboard (as opposed to a group), this code may not work in older versions of Photoshop. I have tested in 2021 and 2024:
#target photoshop
// Checks that the active layer is an artboard
if (isArtboard() === true) {
alert("Select a layer inside the artboard and run the script again...")
} else {
// Check that the current layer is a standard pixel layer
if (app.activeDocument.activeLayer.typename === "ArtLayer" && app.activeDocument.activeLayer.kind === LayerKind.NORMAL) {
// 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;
// Traverse the layers until the artboard is selected
while (isArtboard() === false) {
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;
}
// 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;
} else {
alert("Ensure that a standard pixel layer is selected...");
}
// Return the original ruler units
app.preferences.rulerUnits = originalRulerUnits;
}
function isArtboard() {
// modified from a script by greless with hints from jazz-y!
// returns true or false
try {
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID('layer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));
var options = executeActionGet(r);
return options.hasKey(stringIDToTypeID('artboard')); // test for the required key
} catch (e) {
alert("Error!" + "\r" + e + ' ' + e.line);
}
}