Skip to main content
Participant
December 6, 2024
Answered

Rename artboards in Photoshop using IF clause

  • December 6, 2024
  • 1 reply
  • 960 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...

Jools808080
Known Participant
September 9, 2025

Hi Stephen,

first of all, thanks for the script. I found this thread searching for a way to auto-rename artboards. 

While this isn't precisely what I was looking for, it will do fine if my request isn't possible. 

 

So, since you seem to know what you're doing, here is my question for you: Is it possible to have a script rename artboards with the name of their respective preset? I have a list of artboard presets that i use regularly. It would be super-handy if the artboards were actually named the same as their preset names upon creation, or at least to have an option to rename them all via script. 

 

Thank you in advance. 

Jules

Stephen Marsh
Community Expert
Community Expert
September 9, 2025

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. 


quote

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

 

Thanks for explaining. I don't believe that there's a way to easily automate this from new doc or artboard presets.

 

quote

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. 


By @Jools808080

 

I meant that the artboard preset name is somehow stored in the artboard as a property that can be tested/checked and used. I am not aware of such a property. Once the artboard has been changed via a preset, there is no link or reference back to the preset stored in the artboard that I am aware of.

 

The artboard has a width and height. The width and height of the artboard can be checked via a script, then the script can lookup the name based on these dimensions, as long as there is only one set of unique width and height values to map to the name.

 

So "Artboard 1" having a width of 1125 x 2436 px can be mapped to the name "iPhone X" as long as no other presets use these exact width and height dimensions.

 

This means that if you have 20 presets in use, then the script has to have 20 different conditional checks. The script would then cycle over each artboard check it's width and height and then lookup the corresponding name from a list and rename based on the dimensions.

 

Unless someone else has a better idea?