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

PS Script – creating groups and subgroups based on layer names

Explorer ,
Feb 01, 2023 Feb 01, 2023

Copy link to clipboard

Copied

Hey, I have a script that creates groups and subgroups based on layer names and moves them to the appropriate places. Below is a description of how it works:

 

1. Creates groups based on layer names. The group name is to consist of the layer name up to the first character "_".

2. Creates subgroups in these groups based on the layer names up to the second "_" character

3. Moves all the layers corresponding to the subgroup name to this subgroup

4. Creates an exception for layers with a string in the name "Texture”, “mask” or “Shadow”. Create a group named like this string and move these layers to it.

 

Everything works fine, but I don't know why the script skips a large part of the layers, even though they meet the criteria. I marked the problem on the screen. Could someone suggest how to fix it?

 

 

#target photoshop;

var doc = app.activeDocument;
var layers = doc.layers;
var groups = {};

for (var i = 0; i < layers.length; i++) {
  var layer = layers[i];
  var layerName = layer.name;

  if (layerName.indexOf("Texture") !== -1) {
    if (!groups["Texture"]) {
      groups["Texture"] = doc.layerSets.add();
      groups["Texture"].name = "Texture";
    }
    layer.move(groups["Texture"], ElementPlacement.INSIDE);
    continue;
  } else if (layerName.indexOf("mask") !== -1) {
    if (!groups["mask"]) {
      groups["mask"] = doc.layerSets.add();
      groups["mask"].name = "mask";
    }
    layer.move(groups["mask"], ElementPlacement.INSIDE);
    continue;
  } else if (layerName.indexOf("Shadow") !== -1) {
    if (!groups["Shadow"]) {
      groups["Shadow"] = doc.layerSets.add();
      groups["Shadow"].name = "Shadow";
    }
    layer.move(groups["Shadow"], ElementPlacement.INSIDE);
    continue;
  }

  var firstUnderscoreIndex = layerName.indexOf("_");
  var groupName = layerName.substring(0, firstUnderscoreIndex);
  var secondUnderscoreIndex = layerName.indexOf("_", firstUnderscoreIndex + 1);
  var subgroupName;
  
  if (secondUnderscoreIndex === -1) {
    subgroupName = groupName;
  } else {
    subgroupName = layerName.substring(0, secondUnderscoreIndex);
  }

  if (!groups[groupName]) {
    groups[groupName] = doc.layerSets.add();
    groups[groupName].name = groupName;
  }

  if (!groups[subgroupName]) {
    groups[subgroupName] = groups[groupName].layerSets.add();
    groups[subgroupName].name = subgroupName;
  }

  layer.move(groups[subgroupName], ElementPlacement.INSIDE);
}

 

 

TOPICS
Actions and scripting

Views

1.4K

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
Adobe
Community Expert ,
Feb 03, 2023 Feb 03, 2023

Copy link to clipboard

Copied

Can you post the file having issues?

 

You could resize it down to 1 pixel – it is all about the layers, not the pixel content.

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
Explorer ,
Feb 04, 2023 Feb 04, 2023

Copy link to clipboard

Copied

ok i added the psd file

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 ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied


@KHKH wrote:

ok i added the psd file


There is no psd-file. 

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 ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

@c.pfaffenbichler - The PSD was added to the OP.

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 ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

I do apologize, my mistake. 

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
Explorer ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

@Stephen Marsh hi have you looked at this script? 

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 ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

I have only had limited time, my first look didn't answer why.

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 ,
Aug 12, 2023 Aug 12, 2023

Copy link to clipboard

Copied

@outdoorfurniture 

 

This is a scripting related topic.

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 ,
Aug 12, 2023 Aug 12, 2023

Copy link to clipboard

Copied

This is a scripting related topic, not general use.

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
New Here ,
Aug 17, 2023 Aug 17, 2023

Copy link to clipboard

Copied

yes you are right.

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 ,
Aug 03, 2023 Aug 03, 2023

Copy link to clipboard

Copied

You should iterate backward through the layers (var i = doc.layers.length ... while(i--) ...) because when you create layersets you are creating new layers in the doc and thus your iterator is getting all messed up. That's why sections are skipped. 

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
Explorer ,
Aug 10, 2023 Aug 10, 2023

Copy link to clipboard

Copied

 

 

for (var i = doc.layers.length; i--)

 

 I changed this piece of code but I don't think it was because it doesn't work Could you tell me how to change this code exactly, because I probably misunderstood 😞

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 ,
Aug 10, 2023 Aug 10, 2023

Copy link to clipboard

Copied

Just realized how old this post is, but glad you still need help. Think this should work, but you could $.writeln the current layer name to make sure you're hitting everything 

#target photoshop;

var doc = app.activeDocument;
var layers = doc.layers;
var i = layers.length;
var groups = {};

for (i; i >= 0; i--) {
  var layer = layers[i];
  var layerName = layer.name;

  if (layerName.indexOf("Texture") !== -1) {
    if (!groups["Texture"]) {
      groups["Texture"] = doc.layerSets.add();
      groups["Texture"].name = "Texture";
    }
    layer.move(groups["Texture"], ElementPlacement.INSIDE);
    continue;
  } else if (layerName.indexOf("mask") !== -1) {
    if (!groups["mask"]) {
      groups["mask"] = doc.layerSets.add();
      groups["mask"].name = "mask";
    }
    layer.move(groups["mask"], ElementPlacement.INSIDE);
    continue;
  } else if (layerName.indexOf("Shadow") !== -1) {
    if (!groups["Shadow"]) {
      groups["Shadow"] = doc.layerSets.add();
      groups["Shadow"].name = "Shadow";
    }
    layer.move(groups["Shadow"], ElementPlacement.INSIDE);
    continue;
  }

  var firstUnderscoreIndex = layerName.indexOf("_");
  var groupName = layerName.substring(0, firstUnderscoreIndex);
  var secondUnderscoreIndex = layerName.indexOf("_", firstUnderscoreIndex + 1);
  var subgroupName;
  
  if (secondUnderscoreIndex === -1) {
    subgroupName = groupName;
  } else {
    subgroupName = layerName.substring(0, secondUnderscoreIndex);
  }

  if (!groups[groupName]) {
    groups[groupName] = doc.layerSets.add();
    groups[groupName].name = groupName;
  }

  if (!groups[subgroupName]) {
    groups[subgroupName] = groups[groupName].layerSets.add();
    groups[subgroupName].name = subgroupName;
  }

  layer.move(groups[subgroupName], ElementPlacement.INSIDE);
}

 

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
Explorer ,
Aug 10, 2023 Aug 10, 2023

Copy link to clipboard

Copied

Unfortunately, when I run this script, it immediately throws an error

 

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 ,
Aug 10, 2023 Aug 10, 2023

Copy link to clipboard

Copied

Sorry i should be doc.layers.length - 1

 

A bit foggy from covid I'm afraid

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 ,
Aug 12, 2023 Aug 12, 2023

Copy link to clipboard

Copied

@Ashunya 

 

This is a scripting related topic.

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
New Here ,
Aug 17, 2023 Aug 17, 2023

Copy link to clipboard

Copied

LATEST

lets see.

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