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

Function only working on first pathitem

Engaged ,
Jun 10, 2019 Jun 10, 2019

Copy link to clipboard

Copied

I am running this script I wrote on a simple test file. 2 lines are purple and 2 lines are blue.

The script should move them to the layers based on the color and then format the line weight, cap, and join.

When I run this everything works properly except my function to format the lines. It will change 1 line to the correct settings and not the others.

What am I missing here?

#target illustrator-23

var doc = app.activeDocument;

var allLayers = doc.layers;

var allLines = doc.pathItems;

// Establish color variables globally

var cyanValue;

var magentaValue;

var yellowValue;

var blackValue;

var targetLayer;

// Function to pull all the CMYK stroke color values to a whole number

function getColorValues() {

    cyanValue = (allLines.strokeColor.cyan).toFixed(0);

    magentaValue = (allLines.strokeColor.magenta).toFixed(0);

    yellowValue = (allLines.strokeColor.yellow).toFixed(0);

    blackValue = (allLines.strokeColor.black).toFixed(0);

}

// Function to format a line: No fill, 1.6pt, Miterjoin, Buttcap

function lineFormat() {

    allLines.filled = false;

    allLines.strokeWidth = 1.6;

    allLines.strokeJoin = StrokeJoin.MITERENDJOIN;

    allLines.strokeCap = StrokeCap.BUTTENDCAP;

}

for (var i = 0; i < allLines.length; i++) {

    getColorValues();

    if ((cyanValue == 43 && magentaValue == 76 && yellowValue == 0 && blackValue == 0) || allLines.strokeColor.spot == "[Spot 258 (Dk Purple)]") {

        //alert("This path item is 258 (Dk Purple)");

        targetLayer = doc.layers.getByName("258 (Dk Purple) (Xmsn/Machine/Engine Pump)");

        allLines.moveToBeginning(targetLayer);

        lineFormat();

    }

    else if ((cyanValue == 100 && magentaValue == 0 && yellowValue == 0 && blackValue == 22) || allLines.strokeColor.spot == "[Spot 640 (Blue)]") {

        //alert("This path item is 640 (Blue)");

        targetLayer = doc.layers.getByName("640 (Blue) (Key Power)");

        allLines.moveToBeginning(targetLayer);

        lineFormat();

    }

    //alert("Cyan: " + cyanValue + "\nMagenta: " + magentaValue + "\nYellow: " + yellowValue + "\nBlack: " + blackValue);

}

TOPICS
Scripting

Views

892

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 Expert , Jun 11, 2019 Jun 11, 2019

#target illustrator-23

var doc = app.activeDocument;

var allLayers = doc.layers;

var allLines = doc.pathItems;

// Establish color variables globally (meaning OUTSIDE of the function)

var cyanValue;

var magentaValue;

var yellowValue;

var blackValue;

var targetLayer,p;

// Function to pull all the CMYK stroke color values to a whole number

function getColorValues() {

    cyanValue = (p.strokeColor.cyan).toFixed(0);

    magentaValue = (p.strokeColor.magenta).toFixed(0);

    yellowValue = (p.strokeColor.yellow).t

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 10, 2019 Jun 10, 2019

Copy link to clipboard

Copied

loop backwards

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
Engaged ,
Jun 10, 2019 Jun 10, 2019

Copy link to clipboard

Copied

Oddly when I tried that it fixed the issue of the line weight. All of the lines are assigned the correct attributes from the function. However, then only the first blue and first purple lines are moved to the correct layer. This leaves 1 blue and 1 purple line sitting on an incorrect layer (not being moved).

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 ,
Jun 10, 2019 Jun 10, 2019

Copy link to clipboard

Copied

Can you share an example file (hierarchy, structure, elements) and show us the changed script, please?

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
Engaged ,
Jun 11, 2019 Jun 11, 2019

Copy link to clipboard

Copied

https://drive.google.com/open?id=1eYFo-HK4bI51SlddgL3cMbAKofpPinVZ

There is a link to my Google Drive which contains 3 files.

- The simple test file I am using

- Relayer - Line to Layers Works.jsx  (Line 29 is using a standard for loop).

This script moves the correct colored lines to the proper layer but only changes the line weight of 1 line.

- Relayer - Line Weight Works.jsx (Line 29 is using a reverse for loop)

This script changes the line weight of all of the lines but only moves 1 of each color line to the correct layer.

The only difference between the 2 script files is line 29:

Line to Layers Works

for (var i = 0; i < allLines.length; i++) {

Line Weight Works

for (var i = allLines.length - 1; i >= 0; i--) {

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 ,
Jun 11, 2019 Jun 11, 2019

Copy link to clipboard

Copied

From my phone - without checking your files.

Try:

i> -1; i--)

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
Engaged ,
Jun 11, 2019 Jun 11, 2019

Copy link to clipboard

Copied

Wow now that's some service!

I tried it and still get the same results. The line weights all change but only 2 lines move to the correct layers. The same 2 lines as before.

I appreciate your help on this.... this one has me stumped.

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
Engaged ,
Jun 11, 2019 Jun 11, 2019

Copy link to clipboard

Copied

Ok so I figured out a work around I suppose.

I moved the location of the lineFormat function call to change the line weight BEFORE the line is moved to the correct layer.

If I keep a standard for loop it works perfect. Line weights change and the lines are all moved to the correct layers.

I have left the test files and script as they were.... I didn't update them with the lineFormat before the move.

I feel like it should work no matter if I loop forward or backward. Even with my work around though the reverse loop still doesn't work properly. With the reverse loop still only 2 lines change line weight and move to the correct layer. Even trying to implement the suggestion you made of i> -1; i--)

Here is the updated code:

#target illustrator-23

var doc = app.activeDocument;

var allLayers = doc.layers;

var allLines = doc.pathItems;

// Establish color variables globally (meaning OUTSIDE of the function)

var cyanValue;

var magentaValue;

var yellowValue;

var blackValue;

var targetLayer;

// Function to pull all the CMYK stroke color values to a whole number

function getColorValues() {

    cyanValue = (allLines.strokeColor.cyan).toFixed(0);

    magentaValue = (allLines.strokeColor.magenta).toFixed(0);

    yellowValue = (allLines.strokeColor.yellow).toFixed(0);

    blackValue = (allLines.strokeColor.black).toFixed(0);

}

// Function to format a line: No fill, 1.6pt, Miterjoin, Buttcap

function lineFormat() {

    allLines.filled = false;

    allLines.strokeWidth = 1.6;

    allLines.strokeJoin = StrokeJoin.MITERENDJOIN;

    allLines.strokeCap = StrokeCap.BUTTENDCAP;

}

for (var i = 0; i < allLines.length; i++) {

    getColorValues();

    if ((cyanValue == 43 && magentaValue == 76 && yellowValue == 0 && blackValue == 0) || allLines.strokeColor.spot == "[Spot 258 (Dk Purple)]") {

        //alert("This path item is 258 (Dk Purple)");

        lineFormat();

        targetLayer = doc.layers.getByName("258 (Dk Purple) (Xmsn/Machine/Engine Pump)");

        allLines.moveToBeginning(targetLayer);

    }

    else if ((cyanValue == 100 && magentaValue == 0 && yellowValue == 0 && blackValue == 22) || allLines.strokeColor.spot == "[Spot 640 (Blue)]") {

        //alert("This path item is 640 (Blue)");

        lineFormat();

        targetLayer = doc.layers.getByName("640 (Blue) (Key Power)");

        allLines.moveToBeginning(targetLayer);

    }

    //alert("Cyan: " + cyanValue + "\nMagenta: " + magentaValue + "\nYellow: " + yellowValue + "\nBlack: " + blackValue);

}

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 ,
Jun 11, 2019 Jun 11, 2019

Copy link to clipboard

Copied

#target illustrator-23

var doc = app.activeDocument;

var allLayers = doc.layers;

var allLines = doc.pathItems;

// Establish color variables globally (meaning OUTSIDE of the function)

var cyanValue;

var magentaValue;

var yellowValue;

var blackValue;

var targetLayer,p;

// Function to pull all the CMYK stroke color values to a whole number

function getColorValues() {

    cyanValue = (p.strokeColor.cyan).toFixed(0);

    magentaValue = (p.strokeColor.magenta).toFixed(0);

    yellowValue = (p.strokeColor.yellow).toFixed(0);

    blackValue = (p.strokeColor.black).toFixed(0);

}

// Function to format a line: No fill, 1.6pt, Miterjoin, Buttcap

function lineFormat() {

    p.filled = false;

    p.strokeWidth = 1.6;

    p.strokeJoin = StrokeJoin.MITERENDJOIN;

    p.strokeCap = StrokeCap.BUTTENDCAP;

}

for (var i = allLines.length - 1; i >-1; i--) {

    p = allLines[allLines.length - 1]

    getColorValues();

    if ((cyanValue == 43 && magentaValue == 76 && yellowValue == 0 && blackValue == 0) || p.strokeColor.spot == "[Spot 258 (Dk Purple)]") {

        //alert("This path item is 258 (Dk Purple)");

        targetLayer = doc.layers.getByName("258 (Dk Purple) (Xmsn/Machine/Engine Pump)");

        p.moveToBeginning(targetLayer);

        lineFormat();

    }

    else if ((cyanValue == 100 && magentaValue == 0 && yellowValue == 0 && blackValue == 22) || p.strokeColor.spot == "[Spot 640 (Blue)]") {

        //alert("This path item is 640 (Blue)");

        targetLayer = doc.layers.getByName("640 (Blue) (Key Power)");

        p.moveToBeginning(targetLayer);

        lineFormat();

    }

    //alert("Cyan: " + cyanValue + "\nMagenta: " + magentaValue + "\nYellow: " + yellowValue + "\nBlack: " + blackValue);

}

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
Engaged ,
Jun 12, 2019 Jun 12, 2019

Copy link to clipboard

Copied

pixxxel schubser​ thank you so much!!!

So I see what you did.... but can you explain it to me?

Why did you need to do this in order to make a backwards loop work?

p = allLines[allLines.length - 1]

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 ,
Jun 12, 2019 Jun 12, 2019

Copy link to clipboard

Copied

LATEST

There are other ways to do that. But then you will need another script structure.

Simple said:

Your script find the last (bottom) path (#4) and move it. But now this path is path #1. The line you mentioned will find the the next (now bottom most) path and move it …

… and so on.

(I think you will need another script structure if there are other paths in your document, which not compare the color and not will be moved. But this is always a problem with such extremly simple example files. Not to know what really is in a real document.)

pixxxel schubser​ wrote:

Can you share an example file (hierarchy, structure, elements) …, please?

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