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

Script to merge layers that have the same name into sublayers

Community Beginner ,
Nov 07, 2024 Nov 07, 2024

Copy link to clipboard

Copied

Surely there is a soltion out there but I just coulnt find it. I need a simple script that iterates thru all layers and those with the same name get merge together into a new layer. The original layers end up as sublayers without having their contents comingled or names changed.

 

Screenshot 2024-11-07 at 7.34.32 PM.png

 

 

Screenshot 2024-11-07 at 7.35.29 PM.png

TOPICS
How-to , Scripting

Views

270

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

Enthusiast , Nov 07, 2024 Nov 07, 2024

New layers with sublayers are created on top of other layers.

 

// Script to merge layers that have the same name into sublayers
// https://community.adobe.com/t5/illustrator-discussions/script-to-merge-layers-that-have-the-same-name-into-sublayers/td-p/14968548

function main() {
  if (!documents.length) return;
  var doc = app.activeDocument;
  var layerGroups = collectSameLayers(doc);
  moveLayersToNew(doc, layerGroups);
}

// Collect layers with the same name
function collectSameLayers(doc) {
...

Votes

Translate

Translate
Adobe
Enthusiast ,
Nov 07, 2024 Nov 07, 2024

Copy link to clipboard

Copied

New layers with sublayers are created on top of other layers.

 

// Script to merge layers that have the same name into sublayers
// https://community.adobe.com/t5/illustrator-discussions/script-to-merge-layers-that-have-the-same-name-into-sublayers/td-p/14968548

function main() {
  if (!documents.length) return;
  var doc = app.activeDocument;
  var layerGroups = collectSameLayers(doc);
  moveLayersToNew(doc, layerGroups);
}

// Collect layers with the same name
function collectSameLayers(doc) {
  var layerGroups = [];

  for (var i = 0; i < doc.layers.length; i++) {
    var currLayer = doc.layers[i];
    var currName = currLayer.name;
    var isFound = false;

    for (var j = 0; j < layerGroups.length; j++) {
      if (layerGroups[j].name === currName) {
        layerGroups[j].layers.push(currLayer);
        isFound = true;
        break;
      }
    }

    if (!isFound) {
      layerGroups.push({
        name: currName,
        layers: [currLayer]
      });
    }
  }

  return layerGroups;
}

// Create new layer and move inside original layers
function moveLayersToNew(doc, layerGroups) {
  for (var i = layerGroups.length - 1; i >= 0; i--) {
    var currGroup = layerGroups[i];

    // Only process if there are multiple layers with the same name
    if (currGroup.layers.length > 1) {
      var newLayer = doc.layers.add();
      newLayer.name = currGroup.name;

      for (var j = currGroup.layers.length - 1; j >= 0; j--) {
        currGroup.layers[j].move(newLayer, ElementPlacement.INSIDE);
      }
    }
  }
}

try {
  main();
} catch (e) {}

 

 

 

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 Beginner ,
Nov 07, 2024 Nov 07, 2024

Copy link to clipboard

Copied

Thanks brother ... I'll take it from here.

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 Beginner ,
Nov 08, 2024 Nov 08, 2024

Copy link to clipboard

Copied

How about a little help with including layers that are locked and hidden and maintaining their status once transferred?

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
Enthusiast ,
Nov 08, 2024 Nov 08, 2024

Copy link to clipboard

Copied

LATEST

Yes, I was thinking about this case tonightNow the script can transfer only layer contents via confirmation dialog. https://gist.github.com/creold/6ae88e09836400e43a472db4c0973576

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