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

Script to create layer and already set the color

Community Beginner ,
Mar 31, 2022 Mar 31, 2022

I'm looking for a script that creates a new layer and already sets the color to Light Blue, but that keeps the Light Blue option and doesn't simulate it with Custom.

thiagoleam_0-1648735048204.pngexpand image

 

TOPICS
Scripting
2.0K
Translate
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

Participant , Apr 01, 2022 Apr 01, 2022

I'm not sure if this is what the OP looking for but..

https://www.dropbox.com/s/1etriuidpqqekvg/NewLayerColor_%20AI26%20Release_x64.aip?dl=0

for AI 2022, Win platform.

It forces all new layers to LightBlue color like on  a previous animation.

 

Translate
Adobe
Community Expert ,
Mar 31, 2022 Mar 31, 2022

IMHO we are not able to use predefined colors for layers with scripting. Sorry

Translate
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 ,
Apr 01, 2022 Apr 01, 2022

every once in a while the suggestion of an impossibility lights a fire under me and i want to start figuring it out... The following is NOT good code and should NOT be considered an effective or useful workaround...

 

However... Having said that, there IS actually a way to determine the color of the layer label... Kind of...

 

As we know, when a layer is created, it's automatically assigned a color. i always assumed these were randomized.. but in my quick testing here, it looks like the same values are used every time. So let's say you write a little loop that just creates 10 empty layers.. We can check the "color" of each one of those layers to see whether it looks like what color we want... if it matches, then we can get rid of the rest of the layers so that we're left only with the layer that has the appropriate color. 

 

But what if the color i want isn't used for any of those 10 layers?! Make more, dammit! Make more layers and just keep checking their color until you find the one you want. Except that while testing this, I just learned that the numbers begin to repeat after 27 color combinations... i went through this whole thing of making a "library" of the first 1000 colors used for newly created layers.. and it just keeps repeating after 27. This isnt entirely bad, because tyring to make 1000 layers and then deleting 999 of them isn't a very quick or efficient process... So the good news is you'll never have to process more than 27 layers... The other good news is that that means you get to choose your own custom color, as long as it's one of these 27 colors

 

So, whenever you create a new layer, it will use one of the following colors, according to the index of the layer. If the index of the layer is > 27 (or a multiple of 27), the next layer will use color 0.

 

Choose one of these colors:

Artboard 3.jpgexpand image

 

 

 

Then input the index (number in the top left corner) into the "myChosenIndex" variable in this script:

 

 

#target Illustrator

function setCustomLayerColor()
{
	if(!app.documents.length)
	{
		alert("Please open a document before running this script.");
		return;
	}

	const doc = app.activeDocument;
	const layers = doc.layers;
	var layersToRemove = [];


	var layerColors = [
		{
			"r": 78,
			"g": 127,
			"b": 255
		},
		{
			"r": 255,
			"g": 78,
			"b": 78
		}, {
			"r": 78,
			"g": 255,
			"b": 78
		}, {
			"r": 78,
			"g": 78,
			"b": 255
		}, {
			"r": 255,
			"g": 78,
			"b": 255
		}, {
			"r": 78,
			"g": 255,
			"b": 255
		}, {
			"r": 255,
			"g": 255,
			"b": 78
		}, {
			"r": 127,
			"g": 127,
			"b": 127
		}, {
			"r": 0,
			"g": 0,
			"b": 0
		}, {
			"r": 255,
			"g": 102,
			"b": 0
		}, {
			"r": 0,
			"g": 85,
			"b": 0
		}, {
			"r": 0,
			"g": 153,
			"b": 153
		}, {
			"r": 204,
			"g": 153,
			"b": 102
		}, {
			"r": 153,
			"g": 51,
			"b": 0
		}, {
			"r": 153,
			"g": 51,
			"b": 255
		}, {
			"r": 255,
			"g": 153,
			"b": 0
		}, {
			"r": 0,
			"g": 0,
			"b": 136
		}, {
			"r": 255,
			"g": 153,
			"b": 204
		}, {
			"r": 153,
			"g": 153,
			"b": 255
		}, {
			"r": 153,
			"g": 0,
			"b": 0
		}, {
			"r": 102,
			"g": 102,
			"b": 0
		}, {
			"r": 255,
			"g": 153,
			"b": 153
		}, {
			"r": 153,
			"g": 0,
			"b": 51
		}, {
			"r": 153,
			"g": 204,
			"b": 0
		}, {
			"r": 153,
			"g": 102,
			"b": 0
		}, {
			"r": 102,
			"g": 0,
			"b": 102
		}, {
			"r": 187,
			"g": 187,
			"b": 187
		}, 

	]

	//
	//
	//input the index of the layer color you want here
	var myChosenIndex = 0;
	//
	//

	var ccv = layerColors[myChosenIndex]; //chosen color value


	var foundColor = false;
	var curLay,curColor;
    var counter = 0;
	while(!foundColor && counter < 27)
	{
		curLay = layers.add();
		curColor = curLay.color;
		if(Math.floor(curColor.red) == ccv.r && Math.floor(curColor.green) == ccv.g && Math.floor(curColor.blue) == ccv.b)
		{
			foundColor = true;
		}
		else
		{
			layersToRemove.push(curLay)
            counter++;
		}
        
	}
    

	for(var x=layersToRemove.length-1;x>=0;x--)
	{
		layersToRemove[x].remove();
	}

}
setCustomLayerColor();

 

 

 

boom. "custom" layer colors. Or at the very least, the ability to choose which of the default colors you want to use at a given time. 

Translate
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 ,
Apr 01, 2022 Apr 01, 2022

Yes, I've used a script like that, but it won't be a predefined color, but a custom one...And that bothers me.

Translate
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 ,
Apr 01, 2022 Apr 01, 2022

I've tested the script... If i input "0" for the myChosenIndex variable, it creates a new layer with the color "Light Blue". It's not a custom color. I'm not explicitly setting any color values. I'm just creating empty layers until one of them matches the preset color you want. Then it deletes the ones that were created but not needed. This is exactly like clicking "new layer" in the layers panel until you get the layer color you want. 

 

Please try the code i've provided and show me a screenshot if the layer you're getting has a "custom" color in the "layer info" dialog. In my testing, I can keep running the script over and over and keep getting layers that have the appropriate "Light Blue" named color. 

Translate
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 ,
Apr 01, 2022 Apr 01, 2022

My apologies. There actually was a bug in the script that could result in an infinite loop due to rounding anomolies. i've floored all the values so we can just check integers, and i've added an additional condition to ensure that the loop will indeed exit. 

 

my previous comment with the script included has been updated to reflect these changes.

Translate
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 ,
Mar 31, 2022 Mar 31, 2022

Yesterday I already suggested to take a look at an action I had created a couple of years ago. It still works in the latest Illustrator versions and does exactly what you want.

 

See here:

 

https://community.adobe.com/t5/illustrator-discussions/fix-layer-color/td-p/12846828

 

https://community.adobe.com/t5/illustrator-discussions/layer-colors-set-default/m-p/9184587#M55097

 

Have you already tried it (following the instruction I provided in the older thread)?

 

Translate
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 ,
Mar 31, 2022 Mar 31, 2022

I tested your action, but it does the same thing as the program itself, it will create a layer with a random color...

Translate
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 ,
Mar 31, 2022 Mar 31, 2022

Why bother with Layers if they are all the same color? Just use a single layer.

Translate
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 ,
Mar 31, 2022 Mar 31, 2022

Layers are still useful for locking, hiding, etc. 

 

David Creamer: Community Expert (ACI and ACE 1995-2023)
Translate
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 ,
Mar 31, 2022 Mar 31, 2022

because I have OCD (Obsessive Compulsive Disorder)

Translate
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 ,
Mar 31, 2022 Mar 31, 2022

I realize you are dealing with ODC, but for me, the different layer colors help by giving visual feedback that objects are on the correct layer when I select the object. Something to consider...

 

David Creamer: Community Expert (ACI and ACE 1995-2023)
Translate
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 ,
Mar 31, 2022 Mar 31, 2022
quote

I tested your action, but it does the same thing as the program itself, it will create a layer with a random color...

 

It works fine for me. Perhaps you misunderstood the instructions on how to use the action.

 

You may do a simple test.

 

- Open a new document and change the colour of Layer 1 to "Yellow" in the Layer Options dialog.

 

- Don't close the dialog and immediately choose "Light Blue".

 

- Close the dialog.

 

- Run the action "new_layer_specific_colour" repeatedly.

 

That should always create light blue layers as long as you do not change it to another colour (which will then be the default colour when running the action).

 

Doesn't that work for you?

 

PS: I will modify the action tomorrow, such that the initial manual adjustment will not be required anymore. Will post another download link.

 

Translate
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 ,
Mar 31, 2022 Mar 31, 2022

Could you make a video because I don't understand

Translate
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 ,
Mar 31, 2022 Mar 31, 2022

Sorry, I'm not a good video maker, but as mentioned above, I will upload a modified action set tomorrow.

 

It may be a bit more OCD compatible.

 

Translate
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 ,
Mar 31, 2022 Mar 31, 2022

Thank you!

Translate
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 ,
Apr 01, 2022 Apr 01, 2022

I'm sorry, but apparently I was a bit too optimistic to provide something that works without any kind of manual work.

 

But I still think that you may benefit from the action if you just follow the instructions I already provided.

 

In a nutshell:

 

- Open a new document.

- Go to the Layers palette, double click on the default layer to open the Layer Options dialog.

- In that dialog, take the colour dropdown and change the colour to "Yellow" and, important, don't close the dialog.

- While the dialog is still open, change the colour to "Light Blue", then hit OK to close the dialog.

- Now, use the action to create new layers. They will always be "Light Blue" as long as you do not change the colour of another layer.

 

If you open a new document, you would have to repeat that little setup.

 

For me, that works reliably, and I don't know how to explain it better. Wish someone else could confirm that it works on their machines as well (I'm sure it does).

 

Translate
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
Participant ,
Apr 01, 2022 Apr 01, 2022

Another options is to make a plugin which handles new layer creation event.

But unfortunately I have only a Win platform.

NewLayerColor.gifexpand image

 

Translate
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
Participant ,
Apr 01, 2022 Apr 01, 2022

I'm not sure if this is what the OP looking for but..

https://www.dropbox.com/s/1etriuidpqqekvg/NewLayerColor_%20AI26%20Release_x64.aip?dl=0

for AI 2022, Win platform.

It forces all new layers to LightBlue color like on  a previous animation.

 

Translate
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 ,
Apr 07, 2022 Apr 07, 2022

Very good man, that's exactly what I wanted, you're awesome!

Translate
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 ,
Apr 25, 2022 Apr 25, 2022

by the way do you happen to have this plugin for mac too?

Translate
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
Participant ,
Apr 25, 2022 Apr 25, 2022

I don't have mac.

Translate
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 ,
Feb 02, 2025 Feb 02, 2025
LATEST

Hey can you update this plugin you made so the deafult color would be light red? I tried to copy this into the plugin folder but its not working in the newer versions. Or im copy it into the wrong folder? Thank you

Translate
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