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

change path/vector lineweights using a loop

Community Beginner ,
Dec 04, 2017 Dec 04, 2017

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.selected = false;

                    vectors.fillColor = replaceColor;

                    vectors.strokeWidth = stroke * 0.125;

                    }

TOPICS
Scripting

Views

511

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

Community Beginner , Dec 06, 2017 Dec 06, 2017

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

...

Votes

Translate

Translate
Adobe
Guide ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

Maybe try asking in  the scripting forum

illustrator-scripting

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 ,
Dec 05, 2017 Dec 05, 2017

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

}

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 Beginner ,
Dec 06, 2017 Dec 06, 2017

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.strokeWidth = stroke;

        currentLayer.vectors.strokeWidth = stroke*=0.125;

        }

    }

    }

}

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 ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

What I set up here only loops through few vectors in the first layer:

        doc.layers[0].vectors.strokeWidth = stroke;

        currentLayer.vectors.strokeWidth = stroke*=0.125;

You always are going to doc.layers[0].vectors, maybe this needs to be a removed line.

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 Beginner ,
Dec 06, 2017 Dec 06, 2017

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.strokeWidth = stroke;

            }

            stroke -= 0.125;

        }

    }

}

main();

//////////////////////////////////////////////////////////////////////////////////////////

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 ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

Well what is your vectors,  where does it come from, where should it come from, and where should that come from?

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.

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 Beginner ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

Thanks! It worked

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 Beginner ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

LATEST

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.strokeWidth = stroke;

}

        stroke *= 0.500;

        }

    }

}

main();

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