Need Assistant with a JavaScript for Photoshop
I have a action that creates my folder structure but now I'm wanting it into a script with some logic. I want to create a main group named Workflow and nested inside are 5 groups, with the names Repair, Dodge & Burn, Selective Saturation, Color Corrections and Color Grading. Sometimes I don't use all the groups and delete them but after a revision, I tend to create the missing groups. What I'm after is if the groups exist, do nothing but if they don't, create the missing groups but in that same order.
Here's what I got so far and it creates the groups, but if some are missing, it just creates the groups on the top, not in the order when the script if first ran.
function createWorkflowStructure() {
var doc = app.activeDocument;
// Check if 'Workflow' group already exists
var workflowGroup = getGroupByName(doc, "Workflow");
if (workflowGroup == null) {
// Create 'Workflow' group if it doesn't exist
workflowGroup = doc.layerSets.add();
workflowGroup.name = "Workflow";
// Move 'Workflow' group to the top
workflowGroup.move(doc, ElementPlacement.PLACEATBEGINNING);
}
// Define nested group names in desired order
var nestedGroupNames = ["Repair", "Dodge & Burn", "Selective Saturation", "Color Corrections", "Color Grading"];
// Create missing nested groups within 'Workflow' group, maintaining order
for (var i = 0; i < nestedGroupNames.length; i++) {
createNestedGroupIfMissing(workflowGroup, nestedGroupNames[i]);
}
}
// Helper function to get a group by name
function getGroupByName(doc, name) {
for (var i = 0; i < doc.layerSets.length; i++) {
if (doc.layerSets[i].name === name) {
return doc.layerSets[i];
}
}
return null; // Group not found
}
// Helper function to create a nested group within a parent group if missing
function createNestedGroupIfMissing(parentGroup, groupName) {
var nestedGroup = getGroupByName(parentGroup, groupName);
if (nestedGroup == null) {
// Create nested group if it doesn't exist
nestedGroup = parentGroup.layerSets.add();
nestedGroup.name = groupName;
}
}
// Example usage:
// Call createWorkflowStructure() to ensure the structure exists
createWorkflowStructure();
