Expression Shorts Read External Text Document

January 16th 2012 08:00:50am

Welcome to the first episode of at least five at the moment in the Expression Shorts series. With this series I have put together some short tutorials on various After Effects Expression code that can be very handy.

In these videos I will try not to get too deep into explaining the heavy details of the code, I’ve saved most of that for the article portion of these posts. This way you can directly copy and paste the text if you’d like.

In this episode I explain how to read an external text document. This is a handy bit of code when creating lower thirds style pieces. you can of course use it for other purposes, but it is best suited for this type of work.

Source Text Code:

try{
myPath = "~/Desktop/source.txt";
$.evalFile(myPath);
eval(thisComp.name)[0];
}catch(err){
"MISSING";
}

IMPORTANT:

Text Document Explanation:
We first create a new txt document in TextEdit (mac) or NotePad (pc) that contains our information. The formatting of our information is VERY important. The code is written like so…

var variableName1 = ["Text 0", "Text 1", "Text 2"];

The text in bold above is mandatory and the text in italics is customizable. Your variable name can be what ever you like as long as it DOES NOT start with a number (0, 1, 2, 3, 4, 5, 6, 7, 8 or 9) and it DOES NOT contain a space character. You declare a variable by writing “var” and then traditionally the variable name is written with the first word lowercase and then any additional words starting uppercase like this…

var myVariableNameHere

Ending the variable name with a number will make the comp duplicating process way easier and automatic, so you won’t have to retype the correct name for each comp created. For simplicity I used the following names…

var comp1 = ["Text 0", "Text 1", "Text 2"];
var comp2 = ["Text 0", "Text 1", "Text 2"];
var comp3 = ["Text 0", "Text 1", "Text 2"];
var comp4 = ["Text 0", "Text 1", "Text 2"];

The Array items are added by using double quotes and commas , to separate each item. The entire list is surrounded by brackets [ ]. Each line of code is also ended with a semicolon ; .

var comp1 = ["Text 0", "Text 1", "Text 2"];

The amount of items in the Arrays can be as many as you like, it doesn’t have to be just three items, but you must be aware of what information you plan to to have for your text layers. If you create three text layers and you only have one Array item, there are going to be errors. You would need to fill in something for those other items. This particular Array would result in the first text layer saying “Text 0” and then the second and third layers being blank.

var comp1 = ["Text 0", ", "];

Text Layer Source Text Code Breakdown:

try{
	myPath = "~/Desktop/source.txt";
	$.evalFile(myPath);
	eval(thisComp.name)[0];
}catch(err){
	"MISSING";
}

Lines 1, 5 & 7: The try/catch code is basically saying “try” to run the following code, if something breaks or screws up, “catch” the error “(err)” and run this code instead.

try{

}catch(err){

}

Line 2: “myPath” is a variable we create to hold a value. The value being assigned in this case, is the file path to our txt document called “source.txt” located on our “Desktop”.

myPath = "~/Desktop/source.txt";

Line 3: This line is accessing the system “$” and evaluating “evalFile()” our file. Basically it is reading the contents of our file and evaluating each line as Javascript code instead of just simple text.

$.evalFile(myPath);

Line 4: We then evaluate “eval()” the specific variable that we want to use for our current text layer source text. “thisComp.name” is referencing the current comp name and “[0]” is referencing the specific array item we want. Example: Our current comp is called “comp1” and we want array item number 1 “[0]” (Arrays always start at 0 and not 1, so 0 = 1st item, 1 = 2nd item, 2 = 3rd item and so on.)

eval(thisComp.name)[0];

Line 6: This is our goto value if all else fails. If the main code has any kind of error, the text layer will show this result instead. This can be any text string, and is surrounded by double quotes.

"MISSING";

If you would want the result to be blank text, then you would just place two double quotes with nothing inbetween, followed by a semicolon like so…

";
Checkout more:
Free Function Friday Ep.17 – layerPropMatchNames
Houdini | Loops
Free Function Friday Ep.40 – getLayFXMatchNames
After Effects ExtendScript Training: Ep. 13
Project Info List Exporter After Effects Script