Scripting

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

Recommended Readings

Item

Links

Documentation / Tutorials

  • ​

​

Industry / Case-studies

  • ​

Books

  • ​

Repositories

​

Functions

//String
indexOf() // gets index of character
split("delimiter") // Split a string into an array of substrings:
length //Return the number of characters in a string
match // Search a string for "ain":
slice() // remove characters from front
​
// Arrays
splice() // add elementsto array
toString() // convert to string
​
//REGEX
\t // tab
\n // line
\r // carriage return
\s // whitespace
​
​
Ref: https://www.w3schools.com/jsref/jsref_replace.asp

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

// Selection
var mySelectedComp = app.project.activeItem
var mySelectedLayers = app.project.activeItem.selectedLayers;
​
​
// Setting values
~.property("Position").setValue([120,120]);
​
// making guide layers
.guideLayer = true;
​
// adding expressions
var exp = "string of expression";
~.transform.position.expression =exp;
var proj = app.project;
var comp = app.project.activeItem;

How to use Menu Commands

// Using ID number
var inMenuID = 2000;
app.executeCommand(inMenuID);
​
​
// Using name
app.executeCommand(app.findMenuCommandId("New Project"));

Creating text layer

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

More details:http://docs.aenhancers.com/layers/textlayer/#textlayer​

Creating shape layer

myComp = app.project.activeItem;
myColor = [0,128,255]; // How to set hex code though? Attached to a global fill?
​
// Create shape layer
var myShapeLayer = myComp.layers.addShape();
myShapeLayer.name = ("minRect");
​
// Create shape layer & shape (Ellipse, Rect,)
var shapeGroup = myShapeLayer.property("Contents").addProperty("ADBE Vector Group");
var myRect = shapeGroup.property("Contents").addProperty("ADBE Vector Shape - Rect"); //
myRect.property("Size").setValue([300,100]);
​
// Create stroke properties
var myStroke = shapeGroup.property("Contents").addProperty("ADBE Vector Graphic - Stroke");
myStroke.property("Color").setValue(myColor);
myStroke.property("Opacity").setValue([100]);
myStroke.property("Stroke Width").setValue([2]);
​
​
// Create fill
var myFill = shapeGroup.property("Contents").addProperty("ADBE Vector Graphic - Fill");
myFill.property("Color").setValue(myColor);
myFill.property("Opacity").setValue([100]);
var myRepeater = shapeGroup.property("Contents").addProperty("ADBE Vector Filter - Repeater");
​
// Source: https://forums.creativecow.net/docs/forums/post.php?forumid=227&postid=28156&univpostid=28156&pview=t

Creating a hyperlink

// Source: https://stackoverflow.com/questions/17665920/open-web-page-in-after-effects-with-extendscript
​
// checking that the script can access the network:
​
if (app.preferences.getPrefAsLong("Main Pref Section", "Pref_SCRIPTING_FILE_NETWORK_SECURITY") != 1) {
alert("Please tick the \"Allow Scripts to Write Files and Access Network\" checkbox if Preferences > General");
​
// Then open Preferences > General to let people tick the checkbox
app.executeCommand(2359);
​
// Here you should check again if they ticked it, and choose to continue or stop ...
}
// checking of the OS:
​
var os = system.osName;
if (!os.length) {
// I never remember which one is available, but I think $.os always is, you'll have to check
os = $.os;
}
app_os = (os.indexOf("Win") != -1) ? "Win" : "Mac"
​
//os-dependent system calls:
​
var url = "http://aescripts.com";
​
if (app_os == "Win") {
system.callSystem("explorer " + url);
} else {
system.callSystem("open " + url);
}

Folder creation

basics
parentFolders
createChildFolder
basics
//create a single folder
app.project.items.addFolder("foldername);
// create a folder set
var topFolder = app.project.items.addFolder("Top Level");
topFolder.parentFolder = app.project.rootFolder;
var subFolder = app.project.items.addFolder("Sub Level");
subFolder.parentFolder = topFolder;
​
​
// number of items in project panel
app.project.items.length
parentFolders
// Put comma between folder names & NO space between
var str = "output,main_comps,precomps,assets,reference";
var arr = str.split(",");
​
//Create parent folders
for (i = 0; i < arr.length; i++) {
app.project.items.addFolder("0" + i + "_" + arr[i].toString().toUpperCase());
}
createChildFolder
// Put comma between folder names & NO space between
var str = "output,main_comps,precomps,assets,reference";
var arr = str.split(",");
​
//Create parent folders
for (i = 0; i < arr.length; i++) {
app.project.items.addFolder("0" + i + "_" + arr[i].toString().toUpperCase());
}
app.project.items.addFolder("Ω MESSY");
​
// Create subfolders using loop
var subfoldersStr = "a_LOGO,b_PHOTOGRAPHY,c_FOOTAGE,d_MOVS,e_TEXTURES,f_AUDIO"
var subfoldersArr = subfoldersStr.split(",");
for (i = 0; i < subfoldersArr.length; i++) {
var assetSub = app.project.items.addFolder(subfoldersArr[i].toString());
assetSub.parentFolder = app.project.item(4);
}
​

Instance of

for (var i=0; i<proj.selection.length; i++)
{
if (proj.selection[i] instanceof CompItem)
comps[comps.length] = proj.selection[i];
}
​

Limitations

indexOf()

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

​