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

Rename artboards in Photoshop using IF clause

Community Beginner ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

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

Views

131

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 , 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];
...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

Haha, that code looks familiar...

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

Ah you're the best! Thanks so much 

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

Copy link to clipboard

Copied

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?

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

Copy link to clipboard

Copied

LATEST

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

 

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