-
ExtendsScript – dynamic variable names function loop – Help
Hello,
Hoping someone can point me in the right direction. I have a script that creates a shape layer with a series of horizontal lines on a single shape layer. These lines will represent a grid.
I have everything working, by manually creating each group and name but trying to shorten my code to create the same result using a loop.
Here’s what I have that works;
var addLayer = app.project.item(5).layers;
//Add Headline Grid
var addGrid = addLayer.addShape();
addGrid.label = (13);
addGrid.name = ("headline-text-grid"
var group1 = addGrid.content.addProperty("ADBE Vector Group");
group1.name = "P1";
var myShapeGroup = group1.content.addProperty("ADBE Vector Shape - Group");
var myShape = new Shape();
myShape.vertices = [
[-compWidth / 2, -compHeight / 2],
[compWidth / 2, -compHeight / 2]
];
myShape.closed = false;
// set the value of th path too the shape object
myShapeGroup.property("ADBE Vector Shape").setValue(myShape);
var stroke = group1.content.addProperty("ADBE Vector Graphic - Stroke"); // add a stroke
var p1 = group1.property("ADBE Vector Transform Group").property("ADBE Vector Position");
p1.expression = "[0, effect('Pos-1')('Slider')];"
var group2 = addGrid.content.addProperty("ADBE Vector Group");
group2.name = "P2";
var myShapeGroup2 = group2.content.addProperty("ADBE Vector Shape - Group");
var myShape2 = new Shape();
myShape2.vertices = [
[-compWidth / 2, -compHeight / 2],
[compWidth / 2, -compHeight / 2]
];
myShape2.closed = false;
// set the value of th path too the shape object
myShapeGroup2.property("ADBE Vector Shape").setValue(myShape2);
var stroke2 = group2.content.addProperty("ADBE Vector Graphic - Stroke"); // add a stroke
var p2 = group2.property("ADBE Vector Transform Group").property("ADBE Vector Position");
p2.expression = "[0, effect('Pos-2')('Slider')];"
var group3 = addGrid.content.addProperty("ADBE Vector Group");
group3.name = "P3";
var myShapeGroup3 = group3.content.addProperty("ADBE Vector Shape - Group");
var myShape3 = new Shape();
myShape3.vertices = [
[-compWidth / 2, -compHeight / 2],
[compWidth / 2, -compHeight / 2]
];
myShape3.closed = false;
// set the value of th path too the shape object
myShapeGroup3.property("ADBE Vector Shape").setValue(myShape3);
var stroke3 = group3.content.addProperty("ADBE Vector Graphic - Stroke"); // add a stroke
var p3 = group3.property("ADBE Vector Transform Group").property("ADBE Vector Position");
p3.expression = "[0, effect('Pos-3')('Slider')];"
And here’s my attempt of shortening the above using a loop. The intention is for the loop to repeat 7 times. Can anyone tell me where I’m going wrong?
Many thanks,
Ronan
var addLayer = app.project.item(5).layers;
//Add Headline Grid
var addGrid = addLayer.addShape();
addGrid.label = (13);
addGrid.name = ("headline-text-grid");
function createGrid(){
var group = [];
var myShapeGroup = [];
var myShape = [];
var stroke = [];
var p = [];
for (i = 1; i <=7; i++){
group[i] = addGrid.content.addProperty("ADBE Vector Group");
group[i].name = "P"+[i];
myShapeGroup[i] = group[i].content.addProperty("ADBE Vector Shape - Group");
myShape[i] = new Shape();
myShape[i].vertices = [
[-compWidth / 2, -compHeight / 2],
[compWidth / 2, -compHeight / 2]
];
myShape[i].closed = false;
// set the value of th path too the shape object
myShapeGroup[i].property("ADBE Vector Shape").setValue(myShape[i]);
stroke[i] = group[i].content.addProperty("ADBE Vector Graphic - Stroke"); // add a stroke
p[i] = group[i].property("ADBE Vector Transform Group").property("ADBE Vector Position");
p[i].expression = "[0, effect('Pos-"[i]"')('Slider')];"
}
}