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

Need Assistant with a JavaScript for Photoshop

Participant ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

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

 

 

TOPICS
Actions and scripting , Windows

Views

240

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

Participant , Jun 18, 2024 Jun 18, 2024

Yeah but it's not fully there yet. I still need them nested inside a group called Workflow.

If I change this 

    var repairGroup = createGroupIfNeeded(doc, "Repair");

to this

    var repairGroup = createGroupIfNeeded(workFlowGroup, "Repair");

The groups are placed inside the Workflow folder but then I get extra folders outside the main 😂. Probably something simple for a fix but like I said I'm a noob

// Function to create a new group if it doesn't exist
function createGroupIfNeeded(doc, groupName) {
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

@Unavowed 

 

Will the layer structure vary under the groups?

 

Will there always be a single, Background layer under the groups?

 

Will there always be a single, floating layer under the groups?

 

Will there be a variable amount of layers under the groups (either floating or Background)?

 

A conditional switch block would probably be better than multiple if blocks to check and move the layerSet into the correct position.

 

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
Participant ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

There will always be a Background layer, I do create what I need inside these groups, example, Dodge & Burn will have helper layers that're placed in a group itself and two curves under that, same with the Selective Saturation and Color Corrections, Color Grading mostly has any adjustment layer. 

A conditional switch? If you can make the script smaller/simplier I'm all for it 😊

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 ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

I'm hoping that another scripter adds their thoughts, due to the variable layers this may be hard if using the layer id or index values.

 

Perhaps simply using getByName would be better?

 

What is your scripting skill level and knowledge?

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
Participant ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

Noob tbh. "Perhaps simply using getByName would be better?" Something like this?

// Check if Photoshop is running
if (app && app.documents.length > 0) {
  var doc = app.activeDocument;

  // Function to create a new group if it doesn't exist
  function createGroupIfNeeded(groupName) {
    try {
      // Try to get the group by name
      var group = doc.layerSets.getByName(groupName);
    } catch (e) {
      // Group doesn't exist, so create a new one
      var group = doc.layerSets.add();
      group.name = groupName;
    }
  }

  // Create Workflow group if it doesn't exist
  createGroupIfNeeded("Workflow");

  // Create Repair group if it doesn't exist
  createGroupIfNeeded("Repair");
}

It works I just need to figure out how to place them in order now 😊

 

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 ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

quote

I just need to figure out how to place them in order now 😊

 


By @Unavowed

 

Welcome to layer id and or index.  :]

 

https://gist.github.com/MarshySwamp/ef345ef3dec60a843465347ee6fcae2f

 

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
Participant ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

I got it figured out, somewhat 😊. Thanks. 

I'm still up if anyone wants to share their input though.

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 ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

quote

I got it figured out, somewhat 😊. Thanks. 

 

That's great! I find this challenging.  :]

 

 

quote

I'm still up if anyone wants to share their input though.


By @Unavowed

 

Please do share your final code, I imagine that others will find it hard to comment not knowing what you have done. Did you use ElementPlacement.PLACEBEFORE or ElementPlacement.PLACEAFTER for relative positioning inside the group?

 

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
Participant ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

Yeah but it's not fully there yet. I still need them nested inside a group called Workflow.

If I change this 

    var repairGroup = createGroupIfNeeded(doc, "Repair");

to this

    var repairGroup = createGroupIfNeeded(workFlowGroup, "Repair");

The groups are placed inside the Workflow folder but then I get extra folders outside the main 😂. Probably something simple for a fix but like I said I'm a noob

// Function to create a new group if it doesn't exist
function createGroupIfNeeded(doc, groupName) {
    try {
        // Try to get the group by name
        var group = doc.layerSets.getByName(groupName);
    } catch (e) {
        // Group doesn't exist, so create a new one
        var group = doc.layerSets.add();
        group.name = groupName;
    }
    return group;
}

// Check if Photoshop is running
if (app && app.documents.length > 0) {
    var doc = app.activeDocument;

    // Create Workflow, Repair, and Dodge & Burn groups if they don't exist
    var workFlowGroup = createGroupIfNeeded(doc, "Workflow");
    var repairGroup = createGroupIfNeeded(doc, "Repair");
    var dodgeBurnGroup = createGroupIfNeeded(doc, "Dodge & Burn");
    var selectiveSaturationGroup = createGroupIfNeeded(doc, "Selective Saturation");
    var colorCorrectionsGroup = createGroupIfNeeded(doc, "Color Corrections");
    var colorGradingGroup = createGroupIfNeeded(doc, "Color Grading");

    // Loop through existing layer sets and replace any missing ones in order
    var requiredGroups = ["Color Grading", "Color Corrections", "Selective Saturation", "Dodge & Burn", "Repair"];
    var existingGroups = doc.layerSets;

    for (var i = 0; i < requiredGroups.length; i++) {
        var requiredGroupName = requiredGroups[i];
        var existingGroup = existingGroups[i];

        if (existingGroup && existingGroup.name === requiredGroupName) {
            // Group exists and is in correct order, do nothing
        } else {
            // Group is missing or out of order, recreate it
            var newGroup = createGroupIfNeeded(doc, requiredGroupName);
            // Move the new group to the correct position
            if (existingGroup) {
                newGroup.move(existingGroup, ElementPlacement.PLACEBEFORE);
            } else {
                newGroup.move(doc.layers[doc.layers.length - 1], ElementPlacement.PLACEAFTER);
            }
        }
    }
}

 

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 ,
Jun 18, 2024 Jun 18, 2024

Copy link to clipboard

Copied

LATEST
quote

Probably something simple for a fix but like I said I'm a noob


By @Unavowed

 

 

When it comes to the variable nature of layers, layerSets and ordering via id or index, nothing is simple IMHO!  :]

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