Skip to main content
Participant
December 6, 2024
Answered

Rename artboards in Photoshop using IF clause

  • December 6, 2024
  • 1 reply
  • 952 views

Hi all! 

 

I have a script that automatically renames artboards based on their dimensions, adding 'jpg.100%' and enables generator. 

 

I'm looking to take this one step further and add an 'IF' clause. For example if the dimensions are 1080x1080, then I'd like to rename the artboard 'InstagramSquare'

 

Could anyone help me achieve this? Below is the current script: 

 

function main() {
app.activeDocument.activeLayer = app.activeDocument.layerSets[app.activeDocument.layerSets.length - 1];
// Loop forward over top level layer sets
// for (var i = 0; i < app.activeDocument.layerSets.length; i++) {
// Loop backward over top level layer sets
for (var i = app.activeDocument.layerSets.length - 1; i >= 0; i--) {
try {
app.activeDocument.activeLayer = app.activeDocument.layerSets[i];
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");
activeDocument.activeLayer.name = artBoardRectWidth.value + "x" + artBoardRectHeight.value + ".jpg100%";
app.preferences.rulerUnits = originalRulerUnits;
} catch (e) {}
}
}
app.activeDocument.suspendHistory("Rename All Artboards to Artboard Dimensions", "main()");

 

Correct answer Stephen Marsh

thanks for taking the time to reply. 

My usual workflow would be to create a first artboard by picking a preset in the new file interface, let's say Social Square. I then duplicate the artboard once I have my basic assets placed, by Alt/Option-dragging. Then selecting the artboard in the layers panel I can pick a different preset from the properties panel (or from the context bar if i have the artboard tool selected). Considering I can change the artboard dimensions via the properties panel, I would assume there is some sort of link there? Maybe I misunderstand what you meant. 


@Jools808080 

 

Here is an example using 2 conditions. You would need to expand this 2nd example for each new condition you wish to add with additional "else if" statements.

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/rename-artboards-in-photoshop-using-if-clause/td-p/15023055
*/

#target photoshop

app.activeDocument.suspendHistory("Rename All Artboards to Preset Name", "main()");

function main() {

    app.activeDocument.activeLayer = app.activeDocument.layerSets[app.activeDocument.layerSets.length - 1];

    for (var i = app.activeDocument.layerSets.length - 1; i >= 0; i--) {
        try {

            app.activeDocument.activeLayer = app.activeDocument.layerSets[i];
            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");
            
            // Rename the artboard to the preset name based on matching width and height

            // Condition 1
            if (artBoardRectWidth == 1476 && artBoardRectHeight == 555) {
                activeDocument.activeLayer.name = "JD Desktop Banner";
            // Condition 2
            } else if (artBoardRectWidth == 1080 && artBoardRectHeight == 1920) {
                activeDocument.activeLayer.name = "Phone 1080x1920";
            }
            
            app.preferences.rulerUnits = originalRulerUnits;

        } catch (e) { }
    }
}

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

1 reply

Stephen Marsh
Community Expert
Community Expert
December 6, 2024

Haha, that code looks familiar...

Participant
December 6, 2024

Yes, I should've said, thanks to you! 

Stephen Marsh
Community Expert
Community Expert
December 6, 2024

@Mia32565052d1g2 

 

Here you go, try this:

 

function main() {
    app.activeDocument.activeLayer = app.activeDocument.layerSets[app.activeDocument.layerSets.length - 1];
    // Loop forward over top level layer sets
    // for (var i = 0; i < app.activeDocument.layerSets.length; i++) {
    // Loop backward over top level layer sets
    for (var i = app.activeDocument.layerSets.length - 1; i >= 0; i--) {
        try {
            app.activeDocument.activeLayer = app.activeDocument.layerSets[i];
            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");
            
            if (artBoardRectWidth == 1080 && artBoardRectHeight == 1080) {
                activeDocument.activeLayer.name = "InstagramSquare.jpg100%";
            } else {
                activeDocument.activeLayer.name = artBoardRectWidth.value + "x" + artBoardRectHeight.value + ".jpg100%";
            }
            
            app.preferences.rulerUnits = originalRulerUnits;
        } catch (e) { }
    }
}
app.activeDocument.suspendHistory("Rename All Artboards to Artboard Dimensions", "main()");