Skip to main content
Participant
March 22, 2024
Answered

Scripting Rename layer with Artboard Size Prefix

  • March 22, 2024
  • 2 replies
  • 626 views

Looking for a way to rename a selected layer using the artboard size that the layer is within.
When using Generator, I'm hoping to be able to add that as a folder name to the start of the file name.
I allmost have this script working, but it grabs the first layer size, not the selected size.


var layer = app.activeDocument.layers[0];

Will grab the first layer, not selected layer

var layer = app.activeDocument.activeLayer;
Gives error:
Error 21: undefined is not an object.
Line: 12
-> var width = (ab[2] - ab[0]);

Based on this from @Stephen Marsh 
https://community.adobe.com/t5/photoshop-ecosystem-discussions/automatically-change-artboard-name-to-artboard-dimensions-in-pixels/m-p/13300750#M681172

 

// Retrieve the first layer of the active document
var  layer = app.activeDocument.layers[0];
//var  layer = app.activeDocument.activeLayer;

//alert(layer)


// Calculate the bounds of the artboard containing the selected layer
var ab = artboard_rectangle(layer);

// Calculate half of the artboard width and height
var width = (ab[2] - ab[0]);
var height = (ab[3] - ab[1]);

// Construct a new name for the layer using half of the artboard's width and height
var newName = width + "x" + height;

// Rename the selected layer with the new name
layer.name = newName;

// Check if there is an active document
if (app.activeDocument) {
    // Get the currently selected layer
    var selectedLayer = app.activeDocument.activeLayer;

    if (selectedLayer) {
        // Prefix to be added
        var prefix = width + "x" + height + '/';

        // Check if the layer name is not empty
        if (selectedLayer.name !== "") {
            // Add the prefix to the layer name
            selectedLayer.name = prefix + selectedLayer.name;
        } else {
            alert("Selected layer has no name.");
        }
    } else {
        alert("No layer is selected.");
    }
} else {
    alert("No active document found.");
}


// Function to calculate the bounds of an artboard
function artboard_rectangle(layer) {
    try {        
        var r = new ActionReference();    
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));

        if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);
        else       r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

        var d = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
        var bounds = new Array();

        bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("top"));
        bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("left"));
        bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));
        bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));

        return bounds;
    } catch(e) { 
        alert(e); 
    }
}

 

 

This topic has been closed for replies.
Correct answer Stephen Marsh

Select the layer to prefix with the parent artboard dimensions, then run the script. This variation of the first script ensures that the layer being renamed is within an artboard and not a layer group.

 

var theLayer = app.activeDocument.activeLayer;
app.activeDocument.activeLayer = app.activeDocument.activeLayer.parent;

if (isArtboard() === true) {

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

    // 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");

    app.activeDocument.activeLayer = theLayer;
    app.activeDocument.activeLayer.name = artBoardRectWidth.value + " x " + artBoardRectHeight.value + " " + app.activeDocument.activeLayer.name;

    app.preferences.rulerUnits = originalRulerUnits;

} else {
    app.activeDocument.activeLayer = theLayer;
    alert("The selected layer doesn't appear to be within an artboard!");
}


function isArtboard() {
    // modified from a script by greless with hints from jazz-y!
    try {
        var r = new ActionReference();
        r.putEnumerated(stringIDToTypeID('layer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));
        var options = executeActionGet(r);
        return options.hasKey(stringIDToTypeID('artboard'));
    } catch (e) {
        alert("Error!" + "\r" + e + ' ' + e.line);
    }
}

 

2 replies

Stephen Marsh
Community Expert
Community Expert
March 22, 2024

@sarahdean_au 

 

Select the layer to prefix with the parent artboard dimensions, then run the script. 

 

var theLayer = app.activeDocument.activeLayer;
app.activeDocument.activeLayer = app.activeDocument.activeLayer.parent;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// 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");

app.activeDocument.activeLayer = theLayer;
app.activeDocument.activeLayer.name = artBoardRectWidth.value + " x " + artBoardRectHeight.value + " " + app.activeDocument.activeLayer.name;

app.preferences.rulerUnits = originalRulerUnits;

 

Participant
March 22, 2024

Awesome, this works most of the time, but sometimes gives this error, running 25.0, PS 2024 version.

I'm trying to figure out why, seems to be when a layer inside a folder, inside an artboard is selected, it moves to select the parent folder, then gives this error.


Error 8800: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
- <no additional information available>
Line: 9
-> var artBoardRect = executeActionGet(ref).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));

Stephen Marsh
Community Expert
Community Expert
March 22, 2024

Yes, I didn't account for layerSets within an artboard.

 

The parent of the selected layer would be a layerSet, not the artboard.

 

 

Participant
March 22, 2024

Just clarifying, if there are multiple artboards, multiple sizes, and you have a layer in the second one selected, it will still grab the size from the first layer in the first artboard.

Yet using 'layer = app.activeDocument.activeLayer;' to select the correct layer, gives the undefined error.