Script to create layer and already set the color

Community Beginner ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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.png

 

TOPICS
Scripting

Views

315

Likes

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

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=0for AI 2022, Win platform.It forces all new layers to LightBlue color like on  a previous animation.

Likes

Translate

Translate
Adobe Community Professional ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Apr 01, 2022 Apr 01, 2022

Copy link to clipboard

Copied

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.jpg

 

 

 

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. 

Likes

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

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Apr 01, 2022 Apr 01, 2022

Copy link to clipboard

Copied

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. 

Likes

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
Adobe Community Professional ,
Apr 01, 2022 Apr 01, 2022

Copy link to clipboard

Copied

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.

Likes

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
Adobe Community Professional ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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)?

 

Likes

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

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

Layers are still useful for locking, hiding, etc. 

 

David Creamer
Adobe Certified Instructor, Adobe Certified Professional, and Adobe Certified Expert (since 1995)

Likes

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

Copy link to clipboard

Copied

because I have OCD (Obsessive Compulsive Disorder)

Likes

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
Adobe Community Professional ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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
Adobe Certified Instructor, Adobe Certified Professional, and Adobe Certified Expert (since 1995)

Likes

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
Adobe Community Professional ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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.

 

Likes

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

Copy link to clipboard

Copied

Could you make a video because I don't understand

Likes

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
Adobe Community Professional ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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.

 

Likes

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

Copy link to clipboard

Copied

Thank you!

Likes

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
Adobe Community Professional ,
Apr 01, 2022 Apr 01, 2022

Copy link to clipboard

Copied

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).

 

Likes

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

Copy link to clipboard

Copied

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

But unfortunately I have only a Win platform.

NewLayerColor.gif

 

Likes

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

Copy link to clipboard

Copied

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.

 

Likes

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

Copy link to clipboard

Copied

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

Likes

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

Copy link to clipboard

Copied

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

Likes

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

Copy link to clipboard

Copied

LATEST

I don't have mac.

Likes

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