Skip to main content
andrewc83769988
Participating Frequently
May 4, 2021
Answered

Need help fixing an outdated script in Illustrator

  • May 4, 2021
  • 1 reply
  • 2984 views

I work at a manufacturing company and we set up stamping dies on 12x18" templates in Illustrator.

 

We use a script in Illustrator (which was created in 2007) that creates a quarter inch border around artworks, puts the artworks together like a puzzle on a 12x18" template, and sets the "puzzle" in the top left corner of the template with a quarter inch space on all sides of the template.

 

I tried uploading the script to a backup computer and everything works except it places the artworks above the artboard instead of the top left corner of the template. I am not familiar with writing the code and can no longer get in contact with the script orignator.

 

I've attached two images that show where the template is supposed to be placed, and where the script is placing it. I feel like this should be an easy fix, but with my lack of knowledge with writing code, I am stumped.

This topic has been closed for replies.
Correct answer femkeblanco

just copy and pasted it.

The script originator posted a note at the top of the script saying it was for CS2 and CS3.

I am using the current CS6

 


Changing line 63 corrects the y origin for CS6+. However, it breaks the function which draws the borders. This is dependent on the condition that width is < "lastRow", which is 30. I don't know what this means. Disabling the condition (line 39) gets the script working, but I can't guarantee that it hasn't broken something else.

 

var lastRow = 30;
if (documents.length > 0) {
    var sourceDoc = activeDocument;
    var artLayer = sourceDoc.layers[0];
    if (sourceDoc.pageItems.length > 0) {
        sortWidths();
    } else {
        alert("Please add at least 1 image");
    }
} else {
    var sourceDoc = app.documents.add(DocumentColorSpace.RGB, 864, 1296);
    var artLayer = sourceDoc.layers[0];
    if (sourceDoc.pageItems.length > 0) {
        sortWidths();
    } else {
        alert("Please add at least 1 image");
    }
}
function drawBorder(widthsArr, rowHeight, rowPos) {
    var max = sourceDoc.pageItems.length;
    for (var i = 0; i < max; i++) {
        // get the boundaries, then add .35" to them and create a rectangle
        var gbounds = sourceDoc.pageItems[i].visibleBounds;
        k = widthsArr[i];
        var hTop = rowPos[k];
        var hLeft = gbounds[0];
        var hWidth = gbounds[2] - gbounds[0];
        var hHeight = rowHeight[k];
        var vTop = hTop + 18;
        var vLeft = hLeft - 18;
        var vWidth = hWidth + 36;
        var vHeight = hHeight;  // + 36;
        if (vHeight < 144) {
            vHeight = 144;
        }
        if(vWidth < 72) {
            vWidth = 72;
        }
        // if (k <= lastRow) {
            var rect = artLayer.pathItems.rectangle(vTop, vLeft, vWidth, vHeight);
            //fill is none, send to back
            rect.filled = false;
            rect.zOrder(ZOrderMethod.SENDTOBACK)
        // }
    }
}
function sortWidths() {
    var widthsArr = new Array()
    var max = sourceDoc.pageItems.length;
    for (var i = 0; i < max; i++) {
        widthsArr.push(sourceDoc.pageItems[i].width)
    }
    arrange(widthsArr)
}
function arrange(widthsArr) {
    var done = 0;
    var adjustHeight;
    var rowWidth = new Array(1);
    rowWidth[0] = 36;
    var rowHeight = new Array(1);
    var rowPos = new Array(1);
    rowHeight[0] = 0;
    rowPos[0] = - 36;
    var arrLen = widthsArr.length;
    while (done < arrLen) {
        var max = 0 ;
        var biggest;
        for (var i = 0; i < arrLen; i++) {
            if (widthsArr[i] > max) {
                biggest = i;
                max = widthsArr[i];
            }
        }
        done++;
        widthsArr[biggest] = 0;
        var gbounds = sourceDoc.pageItems[biggest].visibleBounds;
        var hHeight = gbounds[1] - gbounds[3];
        var vHeight = hHeight + 36;
        var hLeft = gbounds[0];
        var hWidth = gbounds[2] - gbounds[0];
        var hTop = gbounds[1];
        var vTop = hTop + 18;
        var vLeft = hLeft - 18;
        var vWidth = hWidth + 36;
        var vHeight = hHeight + 36;
        var width = vWidth;
        var height = vHeight;
        if (width < 72) {
            width = 72;
        }
        if (height < 144) {
            height = 144;
        }
        var numRows = rowWidth.length;
        var newRow = numRows - 1;
        for (var i = 0; i < numRows; i++) {
            if ((rowWidth[i] + width) < sourceDoc.width) {
                sourceDoc.pageItems[biggest].left = rowWidth[i];
                widthsArr[biggest] = i;
                rowWidth[i] = width + rowWidth[i] ;
                if (rowHeight[i] < height && rowHeight[i] != 0 && (adjustHeight > i || adjustHeight == undefined)) {
                    rowHeight[i] = height
                    adjustHeight = i;
                } else if (rowHeight[i]<height) {
                    rowHeight[i] = height
                }
                sourceDoc.pageItems[biggest].top = rowPos[i] - (rowHeight[i]/2 - vHeight/2)
                i=numRows + 1;
            } else if (i == newRow) {
                widthsArr[biggest] = i + 1;
                rowWidth.push(width + 36);
                sourceDoc.pageItems[biggest].left = 36;
                rowPos.push(rowPos[i] - rowHeight[i]);
                rowHeight.push(height);
                var k = i + 1;
                sourceDoc.pageItems[biggest].top = rowPos[k];
                if ((sourceDoc.pageItems[biggest].top - rowHeight[k] - 36) < 0 && lastRow > i) {
                    lastRow = i;
                }
            }
        }
    }
    if (adjustHeight !=undefined) {
        adjustH(adjustHeight, widthsArr, numRows, rowHeight, rowPos);
    } else {
        drawBorder(widthsArr, rowHeight, rowPos)
    }
}
function adjustH(start, list, rows, rowHeight, rowPos) {
    for (var i = start; i < rows; i++) {
        if (i != 0) {
            var k = i - 1;
            rowPos[i] = rowPos[k] - rowHeight[k];
        }
        var listLen=list.length;
        for (var j = 0; j < listLen; j++) {
            if (list[j] == i) {
                var gbounds = sourceDoc.pageItems[j].visibleBounds;
                var hHeight = gbounds[1] - gbounds[3];
                var vHeight = hHeight + 36;
                sourceDoc.pageItems[j].top = rowPos[i] - (rowHeight[i]/2 - vHeight/2)
                var max = sourceDoc.pageItems[j].top - rowHeight[i] - 36;
                if ((sourceDoc.pageItems[j].top - rowHeight[i] - 36) < 0 && lastRow > i) {
                    lastRow = i;
                }
            }
        }
    }
    drawBorder(list, rowHeight, rowPos)
}
function sortNumber(a, b) {
    return b-a
}

 

1 reply

Doug A Roberts
Community Expert
Community Expert
May 4, 2021

You should probably post the script itself too, no?

It might be a simple change from negative to positive x/y position.

andrewc83769988
Participating Frequently
May 4, 2021

Sure thing.

Here is the script.

Doug A Roberts
Community Expert
Community Expert
May 4, 2021

Could you copy and paste it into a post instead of using screenshots?