Scripting
Here you will find javascript snippets that performs one certain small task for learning and application

Recommended Readings

Repositories

Functions

1
//String
2
indexOf() // gets index of character
3
split("delimiter") // Split a string into an array of substrings:
4
length //Return the number of characters in a string
5
match // Search a string for "ain":
6
slice() // remove characters from front
7
​
8
// Arrays
9
splice() // add elementsto array
10
toString() // convert to string
11
​
12
//REGEX
13
\t // tab
14
\n // line
15
\r // carriage return
16
\s // whitespace
17
​
18
​
19
Ref: https://www.w3schools.com/jsref/jsref_replace.asp
Copied!

Useful Extendscript references

Item
Code
Alert
alert("This is an alert")
Select
openDialog("Please select files")
Open file
var spreadsheet = File("/Users/ddu/Desktop/spreadsheet.csv");
Open (Prompt)
var spreadsheet = File.openDialog("Please select .csv file");
Read
var readOK = spreadsheet.open("r");
Prompt (with default value)
var value = prompt("What is the value", 10," This is the title");
​
​

Basics

1
// Selection
2
var proj = app.project;
3
var myComp = app.project.activeItem; // or use "comp"
4
var myLayers = myComp.selectedLayers;
5
​
6
​
7
// Setting values
8
~.property("Position").setValue([120,120]);
9
​
10
// making guide layers
11
.guideLayer = true;
12
​
13
// adding expressions
14
var exp = "string of expression";
15
~.transform.position.expression =exp;
Copied!

How to use Menu Commands

1
// Using ID number
2
var inMenuID = 2000;
3
app.executeCommand(inMenuID);
4
​
5
​
6
// Using name
7
app.executeCommand(app.findMenuCommandId("New Project"));
Copied!

​Adding​

1
//solid
2
app.project.item(index).layers.addSolid(color, name, width, height, pixelAspect[, duration])
3
​
4
//null
5
myNull = app.project.activeItem.layers.addNull();
6
​
7
​
Copied!

Solid Layer

black
comp color
ramp
check error
1
// Black Solid at Comp size
2
​
3
myComp = app.project.activeItem;
4
mySolid = myComp.layers.addSolid([0,0,0], "Solid", myComp.width, myComp.height,1);
Copied!
1
// Create solid named background with composition background color
2
​
3
comp_bg = app.project.activeItem.bgColor;
4
myComp = app.project.activeItem;
5
mySolid = myComp.layers.addSolid(comp_bg, "BG", myComp.width, myComp.height,1);
6
​
Copied!
1
//Create solid named background with a ramp effect
2
​
3
myComp = app.project.activeItem;
4
app.beginUndoGroup("Add Ramp")
5
mySolid = myComp.layers.addSolid([0,.8,.4], "BG", myComp.width, myComp.height,1);
6
mySolid.startTime = 0
7
myEffect = mySolid.property("Effects").addProperty("Ramp");app.endUndoGroup();
Copied!
1
{
2
var activeItem = app.project.activeItem;
3
​
4
if (activeItem == null || !(activeItem instanceof CompItem)) {
5
alert("Select a comp before running this script");
6
} else {
7
// create a red, comp-sized solid layer.
8
theSolid = activeItem.layers.addSolid([1,0,0],"Solid Layer", activeItem.width, activeItem.height, 1);
9
​
10
// find the solid's color array values.
11
c = theSolid.source.mainSource.color;
12
​
13
alert("Created a new solid with color values [" + c[0] + "," + c[1] + "," + c[2] + "]");
14
}
15
}
Copied!

Adjustment Layer

1
// Add Adjustment Layer with Effect.jsx
2
try{
3
var adjLayer = app.project.activeItem.layers.addSolid([0,0,0], "Adjustment Layer", app.project.activeItem.width, app.project.activeItem.height, app.project.activeItem.pixelAspect, app.project.activeItem.duration);
4
adjLayer.adjustmentLayer = true;
5
adjLayer.effect.addProperty("Curves");
6
}
7
catch (e) {
8
alert (e);
9
}
Copied!

Text layer

1
app.project.activeItem.layers.addText('sourceText')
2
myText.property("Source Text").expression = " thisComp.name + \" (\" + (1 + (time/thisComp.frameDuration)) + \")\" "; //add expression
3
myText.property("Position").setValue([172, 644]); //set position to lower right corner
4
​
5
//using an array
6
var content = ['text','text','text'] // read content
7
for(var i=0; i < content.length; i++){
8
app.project.activeItem.layers.addText(content[i]);
9
}
10
​
Copied!

Shape layer

1
myComp = app.project.activeItem;
2
myColor = [0,128,255]; // How to set hex code though? Attached to a global fill?
3
​
4
// Create shape layer
5
var myShapeLayer = myComp.layers.addShape();
6
myShapeLayer.name = ("minRect");
7
​
8
// Create shape layer & shape (Ellipse, Rect,)
9
var shapeGroup = myShapeLayer.property("Contents").addProperty("ADBE Vector Group");
10
var myRect = shapeGroup.property("Contents").addProperty("ADBE Vector Shape - Rect"); //
11
myRect.property("Size").setValue([300,100]);
12
​
13
// Create stroke properties
14
var myStroke = shapeGroup.property("Contents").addProperty("ADBE Vector Graphic - Stroke");
15
myStroke.property("Color").setValue(myColor);
16
myStroke.property("Opacity").setValue([100]);
17
myStroke.property("Stroke Width").setValue([2]);
18
​
19
​
20
// Create fill
21
var myFill = shapeGroup.property("Contents").addProperty("ADBE Vector Graphic - Fill");
22
myFill.property("Color").setValue(myColor);
23
myFill.property("Opacity").setValue([100]);
24
var myRepeater = shapeGroup.property("Contents").addProperty("ADBE Vector Filter - Repeater");
25
​
26
// Source: https://forums.creativecow.net/docs/forums/post.php?forumid=227&postid=28156&univpostid=28156&pview=t
Copied!
1
// Source: https://stackoverflow.com/questions/17665920/open-web-page-in-after-effects-with-extendscript
2
​
3
// checking that the script can access the network:
4
​
5
if (app.preferences.getPrefAsLong("Main Pref Section", "Pref_SCRIPTING_FILE_NETWORK_SECURITY") != 1) {
6
alert("Please tick the \"Allow Scripts to Write Files and Access Network\" checkbox if Preferences > General");
7
​
8
// Then open Preferences > General to let people tick the checkbox
9
app.executeCommand(2359);
10
​
11
// Here you should check again if they ticked it, and choose to continue or stop ...
12
}
13
// checking of the OS:
14
​
15
var os = system.osName;
16
if (!os.length) {
17
// I never remember which one is available, but I think $.os always is, you'll have to check
18
os = $.os;
19
}
20
app_os = (os.indexOf("Win") != -1) ? "Win" : "Mac"
21
​
22
//os-dependent system calls:
23
​
24
var url = "http://aescripts.com";
25
​
26
if (app_os == "Win") {
27
system.callSystem("explorer " + url);
28
} else {
29
system.callSystem("open " + url);
30
}
Copied!

Folder creation

basics
parentFolders
createChildFolder
1
//create a single folder
2
app.project.items.addFolder("foldername);
3
4
// create a folder set
5
var topFolder = app.project.items.addFolder("Top Level");
6
topFolder.parentFolder = app.project.rootFolder;
7
var subFolder = app.project.items.addFolder("Sub Level");
8
subFolder.parentFolder = topFolder;
9
​
10
​
11
// number of items in project panel
12
app.project.items.length
Copied!
1
// Put comma between folder names & NO space between
2
var str = "output,main_comps,precomps,assets,reference";
3
var arr = str.split(",");
4
​
5
//Create parent folders
6
for (i = 0; i < arr.length; i++) {
7
app.project.items.addFolder("0" + i + "_" + arr[i].toString().toUpperCase());
8
}
Copied!
1
// Put comma between folder names & NO space between
2
var str = "output,main_comps,precomps,assets,reference";
3
var arr = str.split(",");
4
​
5
//Create parent folders
6
for (i = 0; i < arr.length; i++) {
7
app.project.items.addFolder("0" + i + "_" + arr[i].toString().toUpperCase());
8
}
9
app.project.items.addFolder("Ξ© MESSY");
10
​
11
// Create subfolders using loop
12
var subfoldersStr = "a_LOGO,b_PHOTOGRAPHY,c_FOOTAGE,d_MOVS,e_TEXTURES,f_AUDIO"
13
var subfoldersArr = subfoldersStr.split(",");
14
for (i = 0; i < subfoldersArr.length; i++) {
15
var assetSub = app.project.items.addFolder(subfoldersArr[i].toString());
16
assetSub.parentFolder = app.project.item(4);
17
}
18
​
Copied!

Instance of

1
for (var i=0; i<proj.selection.length; i++)
2
{
3
if (proj.selection[i] instanceof CompItem)
4
comps[comps.length] = proj.selection[i];
5
}
Copied!

Check if

1
var myLayer = app.project.activeItem.layer(1);
2
var hasEffect = false;
3
​
4
for (var i = 1; i <= myLayer.property("Effects").numProperties; i++) {
5
if (myLayer.property("Effects").property(i).matchName == "ADBE Tile") {
6
alert("It does!");
7
hasEffect = true;
8
break;
9
}
10
}
11
​
12
if (!hasEffect) {
13
myLayer.property("Effects").addProperty("ADBE Tile");
14
}
Copied!

Limitations

indexOf()

1
IndexOf is a string function and doesn't work with arrays anywhere.
2
​
3
​
4
//Source: https://forums.creativecow.net/docs/forums/post.php?forumid=227&postid=37977&univpostid=37977&pview=t
Copied!

Removing

Effects property group [1]

comp
selected layers
1
// removes ALL effects from ALL the layers in active composition
2
​
3
var myComp = app.project.activeItem;
4
var myEffects;
5
​
6
function removeCompFx(){
7
for (var i = 1; i <= myComp.numLayers; i++){
8
try{
9
myEffects = myComp.layer(i).Effects;
10
for (j = myEffects.numProperties; j > 0; j--){
11
myEffects.property(j).remove();
12
}
13
}catch(err){
14
}
15
}
16
}
17
​
18
app.beginUndoGroup("removeFx");
19
removeCompFx();
Copied!
1
// removes ALL effects from SELECTED layers in active composition
2
​
3
var myComp = app.project.activeItem;
4
var mySelectedLayers = myComp.selectedLayers;
5
var myEffects;
6
​
7
function removeSelectedLayersFx(){
8
for (var i = 0; i < mySelectedLayers.length; i++){
9
try{
10
myEffects = mySelectedLayers[i].Effects;
11
for (j = myEffects.numProperties; j > 0; j--){
12
myEffects.property(j).remove();
13
}
14
}catch(err){
15
}
16
}
17
}
18
​
19
app.beginUndoGroup("removeFx");
20
removeSelectedLayersFx();
Copied!

Expressions

1
​
Copied!

Randomize [1]

Position

1
​
2
//Distributes layers in 2D space within comp size
3
var thisComp = app.project.activeItem;
4
var myLayers = thisComp.selectedLayers;
5
var x = thisComp.width;
6
var y = thisComp.height;
7
var randX,randY;
8
​
9
function getRndNum(min, max) {
10
return Math.random() * (max - min) + min;
11
}
12
for(i=0; i<myLayers.length; i++){
13
randX = getRndNum(0,x);
14
randY = getRndNum(0,y);
15
myLayers[i].property("Position").setValue([randX,randY,0]);
16
// myLayers[i].property("Position").setValue([x,y])
17
​
18
}
19
​
Copied!

Misc

1
/* Changes the background color for the active comp to white.
2
Can be customized to any color you'd like by simply changing the [1,1,1] value to reflect the appropriate RGB values...
3
​
4
Example colors:
5
[0,0,0] = Black
6
[0.5,0.5,0.5] = Grey
7
*/
8
​
9
app.project.activeItem.bgColor=[1,1,1];
Copied!

Duplicate selected n times

prompt
15
1
app.beginUndoGroup("Duplicate Selected Layers");
2
var comp = app.project.activeItem;
3
if(comp){
4
var numCopies = prompt("How many copies?",5);
5
if (numCopies){
6
for (var i=0; i < comp.selectedLayers.length; i++) {
7
for (var j=1; j <= numCopies; j++) {
8
comp.selectedLayers[i].duplicate();
9
}
10
}
11
}
12
}
13
app.endUndoGroup;
Copied!
1
app.beginUndoGroup("Duplicate Selected Layers");
2
var comp = app.project.activeItem;
3
if(comp){
4
var numCopies = 15
5
if (numCopies){
6
for (var i=0; i < comp.selectedLayers.length; i++) {
7
for (var j=1; j <= numCopies; j++) {
8
comp.selectedLayers[i].duplicate();
9
}
10
}
11
}
12
}
13
app.endUndoGroup;
Copied!

Kbar Scriptlets

Changes selected Solid source's color to white

1
{
2
var activeItem = app.project.activeItem;
3
​
4
if (activeItem == null || !(activeItem instanceof CompItem)) {
5
alert("Select a comp before running this script");
6
} else {
7
if (activeItem.selectedLayers.length < 1) {
8
alert("Select a solid layer before running this script");
9
} else {
10
solidLayer = activeItem.selectedLayers[0];
11
solidLayer.source.mainSource.color = [1,1,1];
12
alert("Changed solid's color to white.");
13
}
14
}
15
}
Copied!

Change comp bg color to black > white > grey

1
myComp = app.project.activeItem
2
var bg = myComp.bgColor;
3
var n =0 ;
4
​
5
var baseColor = [0,0,0];
6
var col_1 = [1,1,1];
7
var col_2 = [.5,.5,.5];
8
​
9
​
10
if(bg[0]==0&&bg[1]==0&&bg[2]==0) {n=1} // black to white
11
if(bg[0]==1&&bg[1]==1&&bg[2]==1) {n=2} // white to grey
12
if(bg[0]==.5&&bg[1]==.5&&bg[2]==.5) {n=3} // grey to black
13
​
14
​
15
switch(n){
16
case 0:
17
app.project.activeItem.bgColor = baseColor;
18
break;
19
​
20
case 1:
21
app.project.activeItem.bgColor = col_1;
22
break;
23
​
24
case 2:
25
app.project.activeItem.bgColor = col_2;
26
break;
27
​
28
case 3:
29
app.project.activeItem.bgColor = baseColor;
30
break;
31
}
32
​
Copied!

Arrays

Shuffle

1
function shuffleArray(array) {
2
var currentIndex = array.length, temporaryValue, randomIndex;
3
​
4
// While there remain elements to shuffle...
5
while (0 !== currentIndex) {
6
​
7
// Pick a remaining element...
8
randomIndex = Math.floor(Math.random() * currentIndex);
9
currentIndex -= 1;
10
​
11
// And swap it with the current element.
12
temporaryValue = array[currentIndex];
13
array[currentIndex] = array[randomIndex];
14
array[randomIndex] = temporaryValue;
15
}
16
​
17
return array;
18
}
Copied!
Last modified 8mo ago