@V0ID_GUY
The following script isn’t perfect, however, it is the best that I can do for now.
Perhaps a better script writer than me can fix the code as it also indiscriminately processes layer groups and frames (as these are all layerSets)... The isArtboard function that I hacked works when used on a single layer outside of a for loop, but doesn't work when used in a for loop?
Edit: 23rd Februray 2023: Code updated, it should now only rename top-level artboards, not groups or frames.
/*
Rename artboards with custom prefix and sequential numbers.jsx
v1.1 - 23rd February 2023, Stephen Marsh
*/
var sequencePartTwo = activeDocument.activeLayer.name.replace(/(^.+)(_\D+)(0*)(\d+)/, "$2");
var sequenceSuffixLength = 2;
var sequencePartFour = activeDocument.activeLayer.name.replace(/(^.+)(_\D+)(0*)(\d+)/, "$4");
// Backward loop - bottom to top
// for (var i = activeDocument.layerSets.length - 1; i >= 0; i--) {
// Forward loop - top to bottom
for (var i = 0; i < activeDocument.layers.length; i++) {
try {
activeDocument.activeLayer = activeDocument.layers[i];
if (activeDocument.activeLayer.typename === "LayerSet" && isArtboard() === true) {
var sequencePartOne = activeDocument.layers[i].name.replace(/(^.+)(_\D+)(0*)(\d+)/, "$1");
activeDocument.layers[i].name = sequencePartOne + sequencePartTwo + sequenceSuffix(sequencePartFour, sequenceSuffixLength);
sequencePartFour++;
}
} catch (e) {
alert("Error!" + "\r" + e + ' ' + e.line);
}
}
///// Functions /////
function sequenceSuffix(num, digit) {
var tmp = num.toString();
while (tmp.length < digit) {
tmp = "0" + tmp;
}
return tmp;
}
function isArtboard() {
// modified from a script by greless with hints from jazz-y!
// returns true or false
try {
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID('layer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));
var options = executeActionGet(r);
return options.hasKey(stringIDToTypeID('artboard')); // test for the required key
} catch (e) {
//alert(e);
}
}