Skip to main content
DoAdobe
Participant
February 21, 2015
Question

Rename all layers

  • February 21, 2015
  • 1 reply
  • 1014 views

Hi!

I've a problem to solve this issue.

I need to rename a lot of layers (near 1000).

I've found 2 scripts.

First select object in the document, I've little edit it to cycle selection and remove selection. Here the result:

// Adobe Illustrator CC Scripting

// Select and rename layers

var i, j, k, l;

if (app.documents.length > 0) {

  var doc = app.activeDocument;

  if (doc.selection.length == 0) {

  for (i = 0; i < doc.pageItems.length; i++) {

  //Select object

  doc.pageItems.selected = true;


  // HERE I NEED TO RENAME SELECTED LAYER, BEFORE "remove selection".

  //   ????


  //Remove selection

  doc.pageItems.selected = false;

  }

  }

}

Second script make me able to create var "pixelArea" of each object. I need to put that value at the end of each layer name.

I try to put that between in the red area, but give me loop, so Illustrator die.

if (app.documents.length > 0) {

  if (app.activeDocument.selection.length < 1) {

    alert('Select a path first');

  }

  else if (app.activeDocument.selection[0].area) {

    var objects =  app.activeDocument.selection;

  }

  else if (app.activeDocument.selection[0].pathItems) {

    var objects = app.activeDocument.selection[0].pathItems;

  }

  else {

   alert('Please select a path or group.');

  }

  var pixelArea = 0;

  for (var i=0; i<objects.length; i++) {

  if (objects.area) {

  var pixelArea = pixelArea + objects.area;

  if (pixelArea < 0) var pixelArea = -pixelArea;

  // HERE WE HAVE pixelArea VAR. NEED TO ADD THIS VALUE AT THE END OF SELECTED LAYER NAME

  // SOMETHING LIKE THAT: layer.name = layer.name + "-" + pixelArea;

  }

  }

}

Both works (selection/remove selection and area calc, I don't find orking solution for layer name change), but when I combine them together generates the loop.

I'm new with Illustrator scripts, every help could be appreciate.

Really thanks!

This topic has been closed for replies.

1 reply

ThinkingThings
Inspiring
February 21, 2015

Welcome to the forum!

Here's a quick script that might lead you to finding your answer...

    Dim aDoc As Document = app.ActiveDocument

  For Each pi In aDoc.PageItems

    If TypeName(pi.Parent) = TypeName(aDoc.Layers(1)) Then

      Dim layer As Layer = pi.Parent

      'you can rename here

      layer.Name = "New Name"

  End If

Next

Now this is written in .NET so you'll have to translate into JS, but you should get the idea.

Thing is that if you choose to do it the way that you have chosen via pageitems, you will have to find the parent to find the layer that it is on. There might be an easier way if you can elaborate on your objective, at least as far as your first script is concerned...

Does this help?

-TT

DoAdobe
DoAdobeAuthor
Participant
February 21, 2015

Mh, really not. I understand logic but don't know  Illustrator js elements..

I've "solved" with virtual auto-move mouse, I give it loop action to copy/rename/paste code.

Help me this:

var retVal = prompt("Copy this: ", pixelArea);

To make it automatic.

Surely would be better direct script, but the result is all.

ThinkingThings
Inspiring
February 22, 2015

// this should help you with your most recent question
var pixelArea = "244 x 388";
var retValTest = prompt("Copy this: " + pixelArea, pixelArea);

// give this a try -- see if it helps you with your previous (first) question
var aDoc = app.activeDocument;
for (i = 0; i < aDoc.pageItems.length; i++) {
    var pi = aDoc.pageItems;
    if (pi.parent.typename == aDoc.activeLayer.typename) {
                var layer = pi.parent,
                       layerName = layer.name;
                var retVal = prompt("Copy this: " + layerName, layerName);
                if (retVal) {
                    layer.name = retVal;
                }
        }
}

Does this help? :-)    -TT