@Zipser31550168t845
Try this new script, it no longer uses layer name matching and now uses layer order. The onus is on you that the order of layers in both groups is correct.
As a bonus, the layers in the ANIMAL SHAPE group will be renamed to match the layer names in the TEXT group. You're welcome! :]
/*
Link Layers in 2 Specified Layer Groups By Layer Order.jsx
v1.0 - 12th September 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/auto-link-text-layer-with-the-vector-layer-below-it/td-p/14852001
*/
#target photoshop
try {
if (app.documents.length) {
app.activeDocument.suspendHistory("Link Layers in 2 Specified Layer Groups By Layer Order.jsx", "main()");
function main() {
// Get the "TEXT" and "ANIMAL SHAPE" groups
var textGroup = getGroupByName("TEXT");
var animalGroup = getGroupByName("ANIMAL SHAPE");
// Conditional check for the nominated groups
if (textGroup.layers.length == animalGroup.layers.length) {
// Loop through each layer in both groups
var numberOfLayers = Math.min(textGroup.layers.length, animalGroup.layers.length);
for (var i = 0; i < numberOfLayers; i++) {
// Set the "TEXT" group layer variables
var textLayer = textGroup.layers[i];
var textLayerVisibility = textLayer.visible;
// Set the "ANIMAL SHAPE" group layer variables
var animalLayer = animalGroup.layers[i];
var animalLayerVisibility = animalLayer.visible;
// Link the layers in each group using the layer order
textLayer.link(animalLayer);
// Set the layer visibility and rename the animal layer names to match the text layers
textLayer.visible = textLayerVisibility;
var theName = textLayer.name.toLowerCase();
var toSentenceCase = theName.charAt(0).toUpperCase() + theName.slice(1);
animalLayer.name = toSentenceCase + " 1";
animalLayer.visible = animalLayerVisibility;
}
// End of script notification
//app.beep();
//alert("The layers have been linked using the layer order!");
} else {
alert("The number of layers in the nominated groups don't match!")
}
///// FUNCTIONS /////
// Get the group by name
function getGroupByName(groupName) {
return app.activeDocument.layerSets.getByName(groupName);
}
}
} else {
alert("A document must be open to run this script!");
}
} catch (err) {
alert("Error!" + "\r" + err + "\r" + 'Line: ' + err.line);
}