Skip to main content
Participating Frequently
February 4, 2011
Answered

How to set fill, stroke & color to a group

  • February 4, 2011
  • 2 replies
  • 4756 views

First of all, I must say I'm a complete beginner in Javascript so please have patience with me.

What I'm trying to do is to set a lot of groups to specific fill, stroke & color using a function.

var docRef = app.activeDocument;
var Reg = docRef.swatches["[Registration]"].color;
var mm = 2.834645;
var lineWidth=0.12*mm;

group1.stroked=true;
group1.strokeColor = Reg;
group1.filled = false;
group1.strokeWidth = lineWidth;

This works, but I have to this for every group and there are lots of them.

So I tried to make a function:

function setAttributes(temp)

{

temp.stroked=true;
temp.strokeColor = Reg;
temp.filled = false;
temp.strokeWidth = lineWidth;

}

And then call that function for each group:

setAttributes(group1);

setAttributes(group2);

setAttributes(group3);

etc.

Of course, this doesn't work since I have incredible programming skills

Please help.

Thanks!

This topic has been closed for replies.
Correct answer Muppet_Mark-QAl63s

Thanks, this made a big difference.

The only problem is that all the objects that aren't part of a group have no stroke now.

I guess this is because of my messy code.


The script as it is should ONLY change the path items that are in your 'first level' of group items… If you need to change the properties of path items outside of the groups then you could add another loop… Heres one way you add the extra loop:

var docRef = app.activeDocument; // Set variable to your color var reg = docRef.swatches.getByName("[Registration]"); // Set a variable to the document path items collection var pathList = docRef.pathItems; // Loop through this list of objects for (var h = 0; h < pathList.length; h++)     {            // Change the property values      pathList.filled = false;      pathList.stroked = true;      pathList.strokeColor = reg.color;      pathList.strokeWidth = 2;       } // Set a variable to the document groups collection var groupList = docRef.groupItems; // Loop through this list of objects for (var i = 0; i < groupList.length; i++)     {            // Re-use variable for the groups path items collection      pathList = groupList.pathItems;            // Loop through this list of objects      for (var j = 0; j < pathList.length; j++)     {                      // Change the property values           pathList.filled = false;           pathList.stroked = true;           pathList.strokeColor = reg.color;           pathList.strokeWidth = 2;      } } app.redraw();

2 replies

Participating Frequently
February 4, 2011

Thank you! Now it's perfect.

Muppet_Mark-QAl63s
Inspiring
February 4, 2011

No problem… as you can see even though we've only done a couple of things here we've began to repeat syntax. This is where you would start to decide if using 'functions' to handle certain sections of repetition would benefit you…

Muppet_Mark-QAl63s
Inspiring
February 4, 2011

Probably I'll play some more with the code just for learning puropse.

Can you recommend how should I start learning Javascript for Illustrator? (besides this awesome forum and the Illustrator scripting reference documentation from Adobe which is great, but it doesn't feel like it's intended for beginners).

Thanks.


The thing with the guides provided by Adobe is that you have a general understanding of the JavaScript language before you read them… You will need knowledge of working with things like Arrays, Strings, Dates, Numbers, Math and so on… There are lots of free places where you can learn this stuff google will give you lots… You should also look at the Extend Script Tool KIt Guide too as this contains info on dealing with your files and directories… These are NOT part of regular JavaScript…

Muppet_Mark-QAl63s
Inspiring
February 4, 2011

There are a few things here that you have not got right. Your first piece of code does not work for me as 'group1' is an undefined variable? Groups should be referred to as 'groupItems' in JavaScript and the indexing of these collections is 'zero' based so your first group would be…

docRef.groupItems[0];

But before you start changing that… Groups do NOT have a Fill & Stroke as they are made up of a collection of other page items each of which can have differing property types & values… Are your groups made totally from pathItems or are they mixed types?

Participating Frequently
February 4, 2011

Thanks for your reply @Muppet Mark

Sorry, I forgot to write in my post the declaration of group1.

group1 = docRef.groupItems.add();

My groups are made only from pathItems.

If fills and strokes cannot be applied to groups, is there another way to accomplish what I want?

Muppet_Mark-QAl63s
Inspiring
February 4, 2011

If you ONLY want to change the Fill & Stroke property values for pathItems within your groups then you will need to loop through two collections. Firstly you need to loop the groupItems collection of your document then inside of this loop you will need to loop again for all the pathItems contained in each object.

Give me a few minutes and I'll post a sample with a few comments for you…

var docRef = app.activeDocument; // Set variable to your color var reg = docRef.swatches.getByName("[Registration]") // Set a variable to the groups collection var groupList = docRef.groupItems; // Loop through this list of objects for (var i = 0; i < groupList.length; i++)     {            // Set a variable to the path items collection      var pathList = groupList.pathItems;            // Loop through this list of objects      for (var j = 0; j < pathList.length; j++)     {                      // Change the property values           pathList.filled = false;           pathList.stroked = true;           pathList.strokeColor = reg.color;           pathList.strokeWidth = 2;      } } app.redraw();

You will see that in this we have two 'for' loops each of which uses a variable as its increment starting with '0'