Copy link to clipboard
Copied
Hi,
I am trying to change the lineweight using loops so that vectors in each layer is subsequently thinner by a certain amount. Here is a sample of the code that I am working with. Here, I am prompting the user to input a starting lineweight, and then using that for the first layer, and the subsequent layers are multiplied by a factor of 0.125.
So far, it only iterates it twice (there are more than two layers), but all the lineweights are all same value.
///////////////////
var vectors = doc.pathItems;
while (isNaN(stroke) == true) {
var stroke = Number(prompt(doc.layers.name, "Enter lineweight: "));
}
for (var j = 0; j < vectors.length; j++) {
vectors
vectors
vectors
}
Here is what I ended up doing:
#target illustrator
function main() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var stroke = undefined;
var vectors = undefined;
var totalLayers = doc.layers.length;
while (isNaN(stroke) == true) {
stroke = Number(prompt('Enter a numeric lineweight:', "1"));
}
for (var i = 0; i < totalLayers; i++) {
vectors = doc.layers.pathItems;vectors = doc.layers.pathItems;
for (var j = 0; j < vectors.length; j
...Copy link to clipboard
Copied
Maybe try asking in the scripting forum
Copy link to clipboard
Copied
you're assigning the same value to all pathItems,
stroke * 0.125
you have to decrease the value of stroke on each pass.
stroke -= 0.125 (same as stroke = stroke - 0.125)
var doc = app.activeDocument;
var vectors = doc.pathItems;
var stroke = undefined;
while (isNaN(stroke) == true) {
var stroke = Number(prompt('Prompt', "Enter lineweight: "));
}
vectors[0].strokeWidth = stroke; // starting line weight
for (var j = 1; j < vectors.length; j++) {
//vectors
.selected = false; //vectors
.fillColor = replaceColor; vectors
.strokeWidth = stroke -= 0.125; // 2nd path to last, reduce weight by 0.125 }
Copy link to clipboard
Copied
Hi,
I nested this under another loop (for layers), and was hoping one lineweight would apply to all the vectors (pathitems) in each of the layers. What I set up here only loops through few vectors in the first layer:
#target illustrator
function main() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var vectors = doc.pathItems;
var stroke = undefined;
var totallayers = doc.layers.length;
while (isNaN(stroke) == true) {
var stroke = Number(prompt('Prompt', "Enter lineweight: "));
}
for (var i = 0; i < totallayers; i++) {
var currentLayer = doc.layers;
while (isNaN(stroke) == true) {
stroke = Number(prompt('Prompt', "Enter lineweight: "));
}
// Sets initial layer's lineweight to the entered stroke
doc.layers[0].vectors.strokeWidth = stroke; // starting line weight
for (var j = 0; j < vectors.length; j++) {
doc.layers[0].vectors
currentLayer.vectors
}
}
}
}
Copy link to clipboard
Copied
What I set up here only loops through few vectors in the first layer:
doc.layers[0].vectors
currentLayer.vectors
You always are going to doc.layers[0].vectors
Copy link to clipboard
Copied
I removed it but didn't change anything. Here is what I have now:
//////////////////////////////////////////////////////////////////////////////////////////
#target illustrator
function main() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var vectors = doc.pathItems;
var stroke = undefined;
var totallayers = doc.layers.length;
while (isNaN(stroke) == true) {
stroke = Number(prompt('Prompt', "Enter lineweight: "));
}
vectors[0].strokeWidth = stroke;
for (var i = 0; i < totallayers; i++) {
for (var j = 0; j < vectors.length; j++) {
vectors
}
stroke -= 0.125;
}
}
}
main();
//////////////////////////////////////////////////////////////////////////////////////////
Copy link to clipboard
Copied
Well what is your vectors
Right now your vectors is doc.Path items, so it's doing a loop for every layer on all the document's pathItems, and changes all of them to the new stroke variable.
To fix it, you'll have to change the vectors to something like totalLayers.pathItems.
Copy link to clipboard
Copied
Thanks! It worked
Copy link to clipboard
Copied
Here is what I ended up doing:
#target illustrator
function main() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var stroke = undefined;
var vectors = undefined;
var totalLayers = doc.layers.length;
while (isNaN(stroke) == true) {
stroke = Number(prompt('Enter a numeric lineweight:', "1"));
}
for (var i = 0; i < totalLayers; i++) {
vectors = doc.layers.pathItems;vectors = doc.layers.pathItems;
for (var j = 0; j < vectors.length; j++) {
vectors
}
stroke *= 0.500;
}
}
}
main();