Hi @eskaybee875,
if You want to rename all artboards, then You have to loop through all of them, get the dimensions of each and rename it.
This script also checks, if an artboard already has the dimensions in its name and will not rename it, if nothing changed.
I tried to comment the code as best as possible, hopefully it helps.
// Require everything we need to execute our function.
// Do this once outside Your function. You don't need to require
// everything again with each function call.
const photoshop = require("photoshop");
const { action: psAction, app: psApp, constants: psConstants } = photoshop;
const { batchPlay } = psAction;
// The delimiter for Your artboard names.
// We use this, to also replace old artboard dimensions, in case
// any of them get resized.
const DELIMITER = "_";
// A button to call the `rename` function.
const dimensionsButtonEl = document.getElementById("artboard-suffix-btn");
// If button exists, add a click listener with the `renameArtboards`
// callback function.
dimensionsButtonEl?.addEventListener("click", renameArtboards);
/**
* Returns the bounds of an artboard.
*
* @return {Object}
*/
const getArtboardRect = async (docID, artboardID) => {
if (!docID || !artboardID) {
throw new Error('Please provide a document ID and an ' +
'artboard ID');
}
// `batchPlay` requires an array, so it will return an array.
// Store it in the `result` variable.
const result = await batchPlay(
[
{
_obj: "get",
_target: [
{
_property: "artboard",
},
{
_ref: "layer",
_id: artboardID,
},
{
_ref: "document",
_id: docID,
},
],
_options: {
dialogOptions: "dontDisplay",
},
},
],
{}
);
// Return the artboard prop from the first array item of the
// result.
return result[0].artboard.artboardRect;
};
/**
* Helper to escape regex.
* @src: https://stackoverflow.com/a/3561711
*/
function escapeRegex(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
}
/**
* Gets dimensions of all artboards of the document
* and appends them to their name in the layers panel.
*
* @return {void}
*/
const renameArtboards = async () => {
try {
await psCore.executeAsModal(async () => {
// Get the active Document right away.
const activeDoc = psApp.activeDocument;
// Get all artboards of the document.
const artboards = activeDoc.artboards;
// Loop through all artboards.
for (let i = 0; i < artboards.length; i++) {
const artboard = artboards[i];
// Get bounds for this artboard.
const artboardRect = await getArtboardRect(
activeDoc.id, artboard.id
);
// Calculate width and height from bounds.
const artboardW = artboardRect.right - artboardRect.left;
const artboardH = artboardRect.bottom - artboardRect.top;
// Get artboard name before renaming.
const oldArtboardName = artboard.name;
// Create a variable for the new name.
let newName = oldArtboardName;
// Some regex magic to get a sizes pattern, i.e. `_1920x1080`.
// If we have a match, we know, the layer already has a suffix,
// so we can replace it with the actual dimensions.
// By escaping the delimiter, we should be able to use any
// character that we like.
const p = escapeRegex(DELIMITER) + '\\d+\\s*x\\s*\\d+(?:\\s*x\\s*\\d+)?';
const rE = new RegExp(p, 'g');
// Extract last occurrence of old suffix from artboard
// name and also remove `DELIMITER` from string.
const oldSize = oldArtboardName
?.match(rE)
?.pop()
.slice(DELIMITER.length);
// Also remove old suffix from artboard name.
if(oldSize) {
newName = newName.slice(0, -(oldSize.length + DELIMITER.length))
}
// Build the new size string.
const newSize = `${artboardW}x${artboardH}`
// Size didn't change, don't need to rename anything,
// so skip to next artboard.
if(newSize === oldSize) {
console.log(`Nothing changed for artboard '${newName}'.`)
continue;
};
// Concatenate everything into a new string.
artboard.name = `${newName}${DELIMITER}${newSize}`;
}
}, {
commandName: "Rename Artboard"
});
} catch (e) {
console.error("[addSuffix]:\n", e);
// Display the error to the user
// psApp.showAlert(`An error occurred: ${e.message}`);
}
};