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

Need help fixing an outdated script in Illustrator

Community Beginner ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

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.

TOPICS
Feature request , Import and export , Performance , Print and publish , Scripting , Third party plugins , Tools

Views

1.4K

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

Guide , May 04, 2021 May 04, 2021

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) {
      
...

Votes

Translate

Translate
Adobe
Community Expert ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

You should probably post the script itself too, no?

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

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 ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Sure thing.

Here is the script.

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 ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

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

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 ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

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] = sourceDoc.height-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
}

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 ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Please embed the jpgs in the dialog window. The preview is not working. You can also provide a PDF of the script.

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 ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

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

 

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
Guide ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

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
}

 

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 ,
May 05, 2021 May 05, 2021

Copy link to clipboard

Copied

Thank you! Do you think this is the solution?

Should I just create a new Dreamweaver template and paste your script to it?

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
Guide ,
May 06, 2021 May 06, 2021

Copy link to clipboard

Copied

It seems to work for me.  I would test it the usual way you run a script in Illustrator.  I'm unsure what your quesion regarding Dreamweaver is. 

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 ,
May 07, 2021 May 07, 2021

Copy link to clipboard

Copied

LATEST

You're a life saver.

 

Thank you so much!

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