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

Move and Place on Specific Layers

Engaged ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

1.9K

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

Valorous Hero , Oct 01, 2018 Oct 01, 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;

           

...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

So are those spot/global spot color names?

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
Engaged ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

V-

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

Thanks!

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
Valorous Hero ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

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!

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
Engaged ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

so I adjusted the color names but I am getting an error:

function test(){

   var doc = app.activeDocument;

   var searchColors = [

    "4-40",

    "6-32",

    "10-24",

    "1/4-20",

   ];

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

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
Engaged ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

V -

It works!   I missed the Global Spot Color Instruction.

Thank you!

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
Valorous Hero ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

The text .replace("Color ", "") is not needed either, although it's not hurting anything, it could be removed for clarity.

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
Engaged ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

Sorry forgot this part, was there a way to delete empty layers?

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
Valorous Hero ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

I guess you can add this to the end of the snippet:

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

  if(doc.layers.pageItems.length == 0 && doc.layers.length > 1){

    doc.layers.remove();

  }

}

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
Valorous Hero ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

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

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
Advisor ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

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;

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 ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

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];

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
Advisor ,
Jan 14, 2020 Jan 14, 2020

Copy link to clipboard

Copied

LATEST

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

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