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

Incrementing textFrame creation inside a function

Explorer ,
Jun 15, 2017 Jun 15, 2017

Copy link to clipboard

Copied

When I run my script it does exactly like I want... It adds a text frame with the "grid location" for the selected items. My only issue is that it isn't incrementing the text frames. I believe line 42 is the issue....because every time it runs my function it resets the xCoor, yCoor, and inc back to zero. Anyone have an idea of how i can make the text frames not stack on top of each other??

#target illustrator

function findGrid() { 

var d = activeDocument, 

    ps = d.layers['42 Pg Grid Location'].pathItems, 

    l = ps.length, 

    o = 'ABCDEFGHIJKLMNOPQRST...', 

    W, H, 

    rows = columns = 1, 

    origin = d.rulerOrigin, 

    newOrigin = new Array(2), 

    t, b, center, gx, gy, 

    i = 0; 

 

for (; i < l; i++) { 

    if (ps.width == 0) { 

        columns++; 

        !newOrigin[1] && newOrigin[1] = origin[1] + ps.geometricBounds[3]; 

        !H && H = ps.height; 

    } 

    if (ps.height == 0) { 

        rows++; 

        !newOrigin[0] && newOrigin[0] = origin[0] + ps.geometricBounds[2]; 

        !W && W = ps.width; 

    } 

 

d.rulerOrigin = newOrigin; 

W = W / columns; 

H = H / rows;

t = app.selection

b = t.geometricBounds; 

center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2]; 

gx = Math.abs(center[0]) / W; 

gy = Math.abs(center[1]) / H; 

 

d.rulerOrigin = origin; 

//alert('' + o[Math.ceil(gy - 1)] + Math.ceil(gx));

xCoor = 0;

yCoor = 0;

inc = 0;

createGridText = d.textFrames.add();

createGridText.position = [xCoor, yCoor = (inc -= 20)];

createGridText.contents = '' + o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);

//alert(createGridText.contents);

}

for (var z = 0; z < selection.length; z++){

findGrid();

}

Here is what the output looks like....

Capture.JPG

Here is my desired output....

Capture2.JPG

Any help would be appreciated!!

TOPICS
Scripting

Views

875

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

Valorous Hero , Jun 15, 2017 Jun 15, 2017

You are never really doing the (inc -= 20) to create an offset from previous item because your inc = 0; is always defined in the function which is repeated in loop. So your inc is always at -20 for all the items. To decrement your inc, define it outside the function - I'd put it right above the loop which calls the function.

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 15, 2017 Jun 15, 2017

Copy link to clipboard

Copied

Change lines 40-42 from this:

inc = 0; 

createGridText = d.textFrames.add(); 

createGridText.position = [xCoor, yCoor = (inc -= 20)];

to this:

inc = 20; 

createGridText = d.textFrames.add(); 

createGridText.position = [xCoor, (yCoor -= inc) ];

 

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
Explorer ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

williamadowling  wrote

Change lines 40-42 from this:

inc = 0; 

createGridText = d.textFrames.add(); 

createGridText.position = [xCoor, yCoor = (inc -= 20)];

to this:

inc = 20; 

createGridText = d.textFrames.add(); 

createGridText.position = [xCoor, (yCoor -= inc) ];

 

This still keeps everything stacked on top of each other.

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 ,
Jun 15, 2017 Jun 15, 2017

Copy link to clipboard

Copied

You are never really doing the (inc -= 20) to create an offset from previous item because your inc = 0; is always defined in the function which is repeated in loop. So your inc is always at -20 for all the items. To decrement your inc, define it outside the function - I'd put it right above the loop which calls the function.

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
Explorer ,
Jun 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

Perfect! Moved inc =0; outside the function just like you recommended and it works perfectly!

Here is the final code...

#target illustrator

function findGrid() { 

var d = activeDocument, 

    ps = d.layers['42 Pg Grid Location'].pathItems, 

    l = ps.length, 

    o = 'ABCDEFGHIJKLMNOPQRST...', 

    W, H, 

    rows = columns = 1, 

    origin = d.rulerOrigin, 

    newOrigin = new Array(2), 

    t, b, center, gx, gy, 

    i = 0; 

 

for (; i < l; i++) { 

    if (ps.width == 0) { 

        columns++; 

        !newOrigin[1] && newOrigin[1] = origin[1] + ps.geometricBounds[3]; 

        !H && H = ps.height; 

    } 

    if (ps.height == 0) { 

        rows++; 

        !newOrigin[0] && newOrigin[0] = origin[0] + ps.geometricBounds[2]; 

        !W && W = ps.width; 

    } 

 

d.rulerOrigin = newOrigin; 

W = W / columns; 

H = H / rows;

t = app.selection

b = t.geometricBounds; 

center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2]; 

gx = Math.abs(center[0]) / W; 

gy = Math.abs(center[1]) / H; 

 

d.rulerOrigin = origin; 

//alert('' + o[Math.ceil(gy - 1)] + Math.ceil(gx));

xCoor = 0;

yCoor = 0;

createGridText = d.textFrames.add();

createGridText.position = [xCoor, yCoor = (inc -= 20)];

createGridText.contents = '' + o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);

//alert(createGridText.contents);

}

inc = 0;

for (var z = 0; z < selection.length; z++){

findGrid();

}

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 16, 2017 Jun 16, 2017

Copy link to clipboard

Copied

Hi wolfeEdition​,

that's strange. Your code does not work.

Did you running the code from ESTK or directly from Illustrator? Is this all - or only a snippet from a larger script?

Pasting the code in ESTK and select few paths which are located at layer named 42 Pg Grid Location and running your snippet gives  the first error in line #26 d.rulerOrigin = newOrigin;   --> Point value expected

After changing line #10 in /*newOrigin = new Array(2),*/ newOrigin = new Array(0,0),
the script works. but all contents of every new text frames is: undefined-NaN

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
Enthusiast ,
Jun 17, 2017 Jun 17, 2017

Copy link to clipboard

Copied

Seems relative to this post: Re: textFrame resides in a "grid location"

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
Explorer ,
Jun 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

YES! moluapple​ I am building off of that code. I have 2 different accounts.... one is for work and one is for my personal stuff. I accidentally posted this under the wrong profile. Thanks so much for your help on the other post!

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
Explorer ,
Jun 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

I am running the script from ESTK. Sorry I pasted in a working version....this is my final code....

#target illustrator

function findGrid() {

    var d = activeDocument,

        ps = d.layers['42 Pg Grid Location'].pathItems,

        l = ps.length,

        o = 'ABCDEFGHIJKLMNOPQRST...',

        W, H,

        rows = columns = 1,

        origin = d.rulerOrigin,

        newOrigin = new Array(2),

        t, b, center, gx, gy,

        i = 0;

    for (; i < l; i++) {

        if (ps.width == 0) {

            columns++;

            !newOrigin[1] && newOrigin[1] = origin[1] + ps.geometricBounds[3];

            !H && H = ps.height;

        }

        if (ps.height == 0) {

            rows++;

            !newOrigin[0] && newOrigin[0] = origin[0] + ps.geometricBounds[2];

            !W && W = ps.width;

        }

    }

    d.rulerOrigin = newOrigin;

    W = W / columns;

    H = H / rows;

    t = app.selection;

    b = t.geometricBounds;

    center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2];

    gx = Math.abs(center[0]) / W;

    gy = Math.abs(center[1]) / H;

    d.rulerOrigin = origin;

    xCoor = -50;

    yCoor = 0;

    createGridText = d.textFrames.add();

    createGridText.position = [xCoor, yCoor = (inc -= 20)];

    createGridText.contents = '' + o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);

}

  inc = 0;

  incTwo = 0;

  d = activeDocument

for (var z = 0; z < selection.length; z++) {

    xC = -350;

    yC = 0;

    createComponentText = d.textFrames.add();

    createComponentText.position = [xC, yC = (incTwo -= 20)];

    createComponentText.contents = selection.contents;

    findGrid();

}

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 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

LATEST

wolfeEdition  schrieb

I am running the script from ESTK. Sorry I pasted in a working version....this is my final code....

Okay. I see. This snippet required the: Find Text Grid Location.pdf

In other documents the script cannot work.

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
Explorer ,
Jun 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

Is there an easy way to get all of this to happen on a new document instead of the current active document? Meaning it gathers all the information from the current active document and then puts it all on a new document.

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 ,
Jun 19, 2017 Jun 19, 2017

Copy link to clipboard

Copied

You mean save as a copy, then process?

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