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

function for building image frame not working

Community Beginner ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

Copied

Hi there,

 

I am making a templated flyer page in the script. so I want several image placeholders. I decided to build a function to handle this.

 

function addRectangle(width, height, x, y, color, layerName) {
        var myDoc = app.activeDocument;

        var addRect = myDoc.rectangles.add(
            {
                name : layerName,
                geometricBounds : [0, 0, height, width],
                fillColor : color
            }
        );
        addRect.move([x,y]);
    }

this works great until I fill it out.

addRectangle(documentWidth, 1, 0, 0, color, String("Header Rectangle"));

when I do this it builds the image box at the size location color and names the layer. my problem is i cannot figure out how to move the frame or do other things.

for example. if i add var rectangle = (the function above)

then try to move it with 

rectangle.move([2,2]);

 this will not move the rectangle. some when I try to place an image in the frame.

something is going wrong with trying to add a variable to a function I guess.

any ideas?

TOPICS
Scripting

Views

234

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 2 Correct answers

Community Expert , Sep 21, 2020 Sep 21, 2020

In your first example, with the full add function, you are adding the rectangle at the 0,0 coordinates with your geometric bounds, then moving it to the 0,0 coordinate. So that will not move it. If you want a reference to the rectangle, then you need to return the addRect from the function. 

 

 

function addRectangle(width, height, x, y, color, layerName) {
        var myDoc = app.activeDocument;

        var addRect = myDoc.rectangles.add(
            {
                name : layerName,
       
...

Votes

Translate

Translate
Community Expert , Sep 21, 2020 Sep 21, 2020

Got my x and y backward in my last line, and I can't edit my post for some reason. It would be: 

geometricBounds : [y, x, (y + height), (x + width)],

Votes

Translate

Translate
Community Expert ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

Copied

In your first example, with the full add function, you are adding the rectangle at the 0,0 coordinates with your geometric bounds, then moving it to the 0,0 coordinate. So that will not move it. If you want a reference to the rectangle, then you need to return the addRect from the function. 

 

 

function addRectangle(width, height, x, y, color, layerName) {
        var myDoc = app.activeDocument;

        var addRect = myDoc.rectangles.add(
            {
                name : layerName,
                geometricBounds : [0, 0, height, width],
                fillColor : color
            }
        );
        addRect.move([x,y]);
        return addRect; //new line
    }

 

 

So, you could do: 

 

 

var rect = addRectangle(documentWidth, 1, 0, 0, color, String("Header Rectangle"));
rect.move([2,2]);

 

 

Note you are not adding the rectangle to a given layer; you are naming it as a rectangle. If you create more than one rectangle with the same name, you will get an error. If you want to add the rectangle to a given layer, you would make the follwing change to your add line: 

 

 

                itemLayer : layerName, //add it to the item layer
                geometricBounds : [0, 0, height, width],
                fillColor : color

 

 

Technically you don't need your move function at all since you are setting the geometricBounds in your addRectangle function. If you wanted to place it at a given coordinate, just set the bounds appropriately: 

 

geometricBounds : [x, y, (y + height), (x + width)],

 

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 ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

Copied

LATEST

Got my x and y backward in my last line, and I can't edit my post for some reason. It would be: 

geometricBounds : [y, x, (y + height), (x + width)],

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 ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

Copied

If you want to reference the function’s rectangle you have to return it in the function. Also if you are going to move it there is no need for the x,y parameters—you’ll always have to set x,y to zero in order set the width and height. So maybe this works?

 

var myDoc = app.activeDocument;

//set the ruler units
myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES;
myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES;

//the page width
var pw = myDoc.documentPreferences.pageWidth;
var c = myDoc.colors.itemByName ("Black");

var r = addRectangle(pw, 1, c, "Header Rectangle");

r.move([2,2])

function addRectangle(width, height, color, layerName) {
    
    var addRect = myDoc.rectangles.add({
        name : layerName,
        geometricBounds : [0, 0, height, width],
        fillColor : color
    });

    return addRect;
}

 

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 ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

Copied

Hi kyleo,

var rectangle = addRectangle(/*your arguments*/);

cannnot work, because your function does not return the added rectangle. It returns nothing at all.

You can change that if you add this line as last line inside your function:

return addRect;

 

Regards,
Uwe Laubender

( ACP )

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 ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

Copied

Ok. Also note that you always add a rectangle to the first spread in your active document.

 

Regards,
Uwe Laubender

( ACP )

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