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

Rename artboards in Photoshop using IF clause

Community Beginner ,
Dec 06, 2024 Dec 06, 2024

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

 

TOPICS
Actions and scripting
678
Translate
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 2 Correct answers

Community Expert , Dec 06, 2024 Dec 06, 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];
...
Translate
Community Expert , Sep 09, 2025 Sep 09, 2025

@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.a
...
Translate
Adobe
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Haha, that code looks familiar...

Translate
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 ,
Dec 06, 2024 Dec 06, 2024

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

Translate
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 ,
Dec 06, 2024 Dec 06, 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()");
Translate
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 ,
Dec 06, 2024 Dec 06, 2024

Ah you're the best! Thanks so much 

Translate
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 ,
Dec 06, 2024 Dec 06, 2024

Hi again @Stephen Marsh

 

I've tried adding more IF clauses as there are other sizes that will need to be automatically renamed but I'm getting this error:

 

Line: 27
->          } catch (e) { }

 

How do I get around this?

Translate
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 ,
Dec 06, 2024 Dec 06, 2024

@Mia32565052d1g2 – It's hard to know what edits broke the try/catch block without seeing the edited code.

 

How many conditions? Below are links to tutorials on if/else blocks, but you may need to look into switch instead...

 

https://www.w3schools.com/js/js_if_else.asp

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

 

https://www.geeksforgeeks.org/javascript-if-else/

 

https://www.freecodecamp.org/news/javascript-if-else-and-if-then-js-conditional-statements/

 

Translate
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
Participant ,
Sep 09, 2025 Sep 09, 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

Translate
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 ,
Sep 09, 2025 Sep 09, 2025

Once created, what is the correlation between the preset names and the artboard sizes etc? It's probably going to need a lookup for a unique size vs. the preset name.

 

Can you provide screenshots of the presets and the results? Perhaps the presets can be applied in another way.

Translate
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
Participant ,
Sep 09, 2025 Sep 09, 2025

some of the presets are standard dimensions, like social media square (1080x1080px) while others are proprietary website banners (e.g. 1476x555px). I have these presets saved in my Photoshop applcation with the correct names (desktop banner, news article header etc.), so I don't actaully require the size in the name. My question is really more, whether you'd know how to integrate access to the presets file (i'm not sure where it is stored within Photoshop, nor how to reference it in a script) into a renaming script. 

Translate
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
Participant ,
Sep 09, 2025 Sep 09, 2025

Screenshot 2025-09-09 at 09.46.38.png

 

Translate
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 ,
Sep 09, 2025 Sep 09, 2025

AFAIK, once created, there is no link or relationship between the preset and the corresponding artboard. 

So if an Artboard had dimensions of 800 x 800 px and this matched a preset named "Square 3" then the script could rename all Artboards matching this criteria.

 

Otherwise, it would need to happen when the preset is applied. I haven't tested this. Where and how do you select the preset? Do you mean when creating a new document? Your screenshot was cropped so I don't have any context.

Translate
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
Participant ,
Sep 09, 2025 Sep 09, 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. 

Translate
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 ,
Sep 09, 2025 Sep 09, 2025
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?

Translate
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 ,
Sep 09, 2025 Sep 09, 2025

@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

Translate
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
Participant ,
Sep 09, 2025 Sep 09, 2025

Stephen, you're a star! I added a few more conditions according to my presets, and just tried it: works a charm! Thanks so much! 

Translate
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 ,
Sep 09, 2025 Sep 09, 2025
LATEST
quote

Stephen, you're a star! I added a few more conditions according to my presets, and just tried it: works a charm! Thanks so much! 


By @Jools808080


That's fantastic! Those nested open { and close } braces can be painful, glad you got them right!

Translate
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