-
Advice on Script that Creates Keys of a Keyboard
Hi all,
I’m new to javascript and script writing and have been wrting some test scripts. Below is a short script that creates 3 rows of the keys on a keyboard. In the javascript course I was going through they constantly said to not repeat yourself and this script definitely is repetitive.
After line 19 everything is pretty much the same so I put in a comment “///You Can Stop Reading Here” so you don’t have to read through the whole thing. The only change is
How can I use fewer loops? I used two for loops for every row- one loop created the text layers (searched array) and the other repositioned them.(searched the comp item collection)
Is it possible to create layers from an array and position them at the same time?I’m trying hard to improve my scripting so any advice would be greatly appreciated.
Thanks in advance for any help!
-Tim
app.beginUndoGroup("Keyboard Setup");var items = app.project.items;
{
var asdfComp = items.addComp("asdf",1920,1080,1,20,24);
var asdf = ["Caps Lock","A","S","D","F","G","H","J","K","L",";","'","Enter","4","5","6"];
asdf.reverse(); //reversed the array so After Effects would create them in the correct order
var aLength = asdf.length;
for(i=0;i<aLength;i++){ //creates text layers
var text = asdf[i];
asdfComp.layers.addText(text);
};for(i=1;i<=aLength;i++){ //repositions the text layers in the new comp
var currentPos = asdfComp.layer(i).property("Position");
var xPos = (i*75)
var newPos = currentPos.setValue([xPos,540]);
};
}; ////// You can Stop Reading Here{
var qwerComp = items.addComp("qwer",1920,1080,1,20,24);
var qwer = ["Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","|","Delete","End","Page Down","7","8","9","+"];
qwer.reverse();
var qLength=qwer.length;for(i=0;i<qLength;i++){
var text = qwer[i];
qwerComp.layers.addText(text);
};for(i=1;i<=qLength;i++){
var currentPos = qwerComp.layer(i).property("Position");
var xPos = (i*75)
var newPos = currentPos.setValue([xPos,340]); // This changes the Position of the Text Layers
};
};
{
var zxcvComp = items.addComp("zxcv",1920,1080,1,20,24);
var zxcv = ["Shift","Z","X","C","V","B","N","M",",",".","/","Shift"," ","1","2","3","Enter"]
zxcv.reverse();
var zLength=zxcv.length;for(i=0;i<zLength;i++){
var text = zxcv[i];
zxcvComp.layers.addText(text);};
for(i=1;i<=zLength;i++){
var currentPos = zxcvComp.layer(i).property("Position");
var xPos = (i*75)
var newPos = currentPos.setValue([xPos,740]);
};
};Tim Franklin
Watchout System Programmer/Compositor – CBS Sports
Freelance Visual Effects Artist/ Video Editor
http://www.TimSFranklin.com