• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Scripting Rename layer with Artboard Size Prefix

Community Beginner ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

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_A_Marsh 
https://community.adobe.com/t5/photoshop-ecosystem-discussions/automatically-change-artboard-name-to...

 

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

 

 

TOPICS
Actions and scripting

Views

153

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

correct answers 1 Correct answer

Community Expert , Mar 22, 2024 Mar 22, 2024

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 Run
...

Votes

Translate

Translate
Adobe
Community Beginner ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

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.

Votes

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
Community Expert ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

@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;

 

Votes

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
Community Expert ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

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);
    }
}

 

Votes

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
Community Beginner ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

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

Votes

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
Community Expert ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

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

 

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

 

2024-03-23_10-31-06.png

 

Votes

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
Community Expert ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

@sarahdean_au 

 

Select the layer to prefix with the parent artboard dimensions, then run the script. The following variation of the previous script will check inside a single-level layer group within the artboard (not for nested groups within groups).

 

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

if (isArtboard() === true) {
    layerRenamer();

} else if (app.activeDocument.activeLayer.typename === "LayerSet") {
    app.activeDocument.activeLayer = app.activeDocument.activeLayer.parent;
    layerRenamer();

} else {
    app.activeDocument.activeLayer = theLayer;
    alert("The selected layer may be within a nested layer group or not within an artboard...");
}


function layerRenamer() {
    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;
}

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);
    }
}

 

Votes

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
Community Beginner ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

Awesome, thanks, this is super useful.

 

Votes

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
Community Expert ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

LATEST

Select the layer to prefix with the parent artboard dimensions, then run the script. The following version should work with nested layer sets within the parent artboard:

 

var theLayer = app.activeDocument.activeLayer;

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

function layerRenamer() {
    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;
}

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);
    }
}

function selectRootLayerSet() {
    var currentLayer = app.activeDocument.activeLayer;
    function findRootLayerSet(layer) {
        if (layer.parent.typename == "LayerSet") {
            findRootLayerSet(layer.parent);
        } else {
            // Root layer set found, select it
            app.activeDocument.activeLayer = layer;
        }
    }
    // Start searching for the root layer set
    findRootLayerSet(currentLayer);
}

 

Votes

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