Skip to main content
Inspiring
October 1, 2018
Answered

Move and Place on Specific Layers

  • October 1, 2018
  • 3 replies
  • 2396 views

Morning!

Thanks to the helpfulness of everyone on the forums I have a script that will create layers with specific names, locks the bottom Layer!  I am hoping I can take this script even further if possible.

The script creates 5 new layers and I would love it if we could move elements by color to a specific layer

If "Color A" filled objects exist Select All "Color A" and move to Layer "A"

If "Color B" filled objects exist Select All "Color B" and move to Layer "B"

If "Color C" filled objects exist Select All "Color C" and move to Layer "C"

If "Color D" filled objects exist Select All "Color D" and move to Layer "D"

Then Delete all Empty Layers

var main = function(layerCount) { 

 

var doc, layers; 

 

if ( !app.documents.length ) { 

alert("This scripts needs an open document !"); 

return; 

 

//The doc 

doc = app.activeDocument; 

//Adding useful layers 

addLayers ( doc ); 

 

// WILL CREATE ANY NUMBER OF NAMED LAYERS === 'NAME',

var addLayers = function ( doc ) { 

var layers = getLayersDB ( doc ); 

var layerNames = [ 

'A', 

'B',

'C',

'D',

'BASE',

], 

name; 

 

while ( name = layerNames.pop() )  !layers[name]  && addLayer ( doc, name ); 

}; 

 

var addLayer = function ( doc, name ) { 

doc.layers.add().name = name; 

 

var getLayersDB = function ( doc ) { 

var db = {}, layers = doc.layers, n = layers.length, layer; 

 

while ( n-- ) { 

layer = layers

db[layer.name] = layer; 

return db; 

}

 

main(); 

// LOCK BOTTOM LAYER -= ONLY WORKS IF THERE ARE MULTIPLE LAYERS EXIST =-

if (documents.length > 0) {

countOfLayers = activeDocument.layers.length;

}

if (countOfLayers > 1) {

bottomLayer = activeDocument.layers[countOfLayers-1];

}

bottomLayer.locked = true;

As always thank you for your thoughts and support!

This topic has been closed for replies.
Correct answer Silly-V

Well you can give this a try, you have to have global/spot colors named "Color A", etc and layers named with just the letter "A", etc.

#target illustrator

function test(){

   var doc = app.activeDocument;

   var searchColors = [

    "Color A",

    "Color B",

    "Color C",

    "Color D",

    "Color E",

   ];

   var thisSwatchColorSpot, thisSelection, thisSearchColor;

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

       thisSearchColor = searchColors;

       try {

           doc.selection = null;

            thisSwatchColorSpot = doc.spots.getByName(thisSearchColor);

            doc.defaultFillColor = doc.swatches[thisSwatchColorSpot.name].color;

            app.executeMenuCommand("Find Fill Color menu item");

            for(var j = 0; j < doc.selection.length; j++){

                thisSelection = doc.selection;

                thisSelection.move(doc.layers[thisSearchColor.replace("Color ", "")], ElementPlacement.PLACEATEND);

            }

       } catch (e) {

           alert("Error with color '" +  + "'\n" + e);

       }

   }

};

test();

3 replies

CarlosCanto
Community Expert
Community Expert
January 7, 2020

yeah, the new format messed all existing scripts, but not the line you posted.

 

change this

thisSearchColor = searchColors;

 

to this

thisSearchColor = searchColors[i];

 

and this

thisSelection = doc.selection;

 

to this

thisSelection = doc.selection[j];

schroef
Inspiring
January 14, 2020

Indeed, i was just curious what it was for. Seems to be left-over code

Silly-V
Silly-VCorrect answer
Legend
October 1, 2018

Well you can give this a try, you have to have global/spot colors named "Color A", etc and layers named with just the letter "A", etc.

#target illustrator

function test(){

   var doc = app.activeDocument;

   var searchColors = [

    "Color A",

    "Color B",

    "Color C",

    "Color D",

    "Color E",

   ];

   var thisSwatchColorSpot, thisSelection, thisSearchColor;

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

       thisSearchColor = searchColors;

       try {

           doc.selection = null;

            thisSwatchColorSpot = doc.spots.getByName(thisSearchColor);

            doc.defaultFillColor = doc.swatches[thisSwatchColorSpot.name].color;

            app.executeMenuCommand("Find Fill Color menu item");

            for(var j = 0; j < doc.selection.length; j++){

                thisSelection = doc.selection;

                thisSelection.move(doc.layers[thisSearchColor.replace("Color ", "")], ElementPlacement.PLACEATEND);

            }

       } catch (e) {

           alert("Error with color '" +  + "'\n" + e);

       }

   }

};

test();

schroef
Inspiring
January 6, 2020

Seems like with the new forum lots of scripts pasted here are broken.

 

It looks like some parts are missing, this var is declared, but nothing is done with it???

doc.defaultFillColor = doc.swatches[thisSwatchColorSpot.name].color;
Silly-V
Legend
October 1, 2018

So are those spot/global spot color names?

Inspiring
October 1, 2018

V-

The colors used have been given Specific Names: 4-10, 6-32, 10-24, 1/4-20.  

Thanks!

Silly-V
Legend
October 1, 2018

Aha, if your layers are named the same names, you can change all the items in the top array where the "Color A" strings are all written, and replace them with your special names "4-40". It should still work just with that edit. Try it out let's see what happens!