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?
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,
...
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)],
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)],
Copy link to clipboard
Copied
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)],
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;
}
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 )
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 )