Expressions and External Documents – Revised for CS4

In the tutorial Expressions and External Documents, I showed some ideas on how to use external documents to drive content inside After Effects. The feedback that I’ve gotten from the community is that this technique can be a real timesaver in projects that require a lot of repetitive content, such as bumpers, lower thirds, interstitials, etc.

In the tutorial, with my own attempt at the idea, I fell short on a couple of challenges.  First, because After Effects uses different address structures for Windows and Mac OS X, it’s a little tricky to make one expression that works on both platforms.  Second, as I’ve later found out, the #include function was disabled in AE CS4.

Fortunately, scripting and expressions guru Lloyd Alvarez stepped in to fill in the gaps and provide a solution that works in both CS3 and CS4, as well as both platforms. Let’s take a look at the full Source Text expression:

if ($.os.indexOf("Mac") != -1)
myPath = "/Expressions/";
else
myPath = "file://c:\\\\Expressions\\";
myPath += "data.txt";
$.evalFile (myPath);
eval(thisComp.name);

Let’s take a look at what is going on here.

if ($.os.indexOf(“Mac”) != -1)

In this first line,  $.os is equal to the name of our current operating system. For example, on my computer this displays “Macintosh OS 10.5.6″.  indexOf() simply looks for the content in parentheses. If it finds this, like the letters “Mac”, it is equal to a value of 0. If it is not found, the value is -1. If you’ve not used it before, the term != means “does not equal”.

Therefore, this is an if/else condition that says “if the current OS contains the word ‘Mac’,then do the following. So, we have things set up for both Mac and Windows, and we can handle things accordingly.

The next line is what runs if the OS is Mac based, and it establishes the initial path of where to look for the external document.

myPath = “/Expressions/”;

If the OS is not a Mac, and is therefore Windows based, the path will be formatted for Windows.  Note that in Windows, two slashes: \\  need to be supplied to denote a directory, not just one.

else
myPath = “file://c:\\\\Expressions\\”;

Now that we have the path set up, let’s assume that the text document that has our data is called “data.txt”.  What can be done at this point is to add the path and filename together like this:

myPath += “data.txt”;

This is the equivalent of myPath = mypath + “data.txt”;

Then, an alternative to #include is $.evalFile, which fortunately works in CS3 and CS4 as a way to look at the contents of an external file. Then, just like in the tutorial, we can use eval() to have the comp name pass as the the value for the source text, as the last value in the expression. Assuming there is a variable in the data file that matches the comp name, it will be displayed as the source text.

$.evalFile (myPath);
eval(thisComp.name);

To have the expression wrapped inside try/catch to eliminate error messages (such as in the case of the text document not having the correct scene number), here is a version like that:

try{
if ($.os.indexOf(“Mac”) != -1)
myPath = “/Expressions/”;
else
myPath = “file://c:\\\\Expressions\\”;
myPath += “data.txt”;
$.evalFile (myPath);
eval(thisComp.name);
}catch(err){“Not Found”}

For some complete training on After Effects Expressions, and to support the site, take a look at my After Effects Expressions training series.expressions

Download a preformatted text document and spreadsheet

23 Responses to “Expressions and External Documents – Revised for CS4”

  1. Chaabane says:

    awsome, but i have a question ,Why in the If condition you used “-1″, i believe it works just with 1 and 0 values ?

  2. Harry says:

    It’s a matter of what indexOf returns as a value. indexOf() simply looks for the content in parentheses. If it finds this, like the letters “Mac”, it is equal to a value of 0. If it is not found, the value is -1.

  3. Rob says:

    At first the evalFile function was working great for me, and once i closed my project
    and reopened it, it says the file or directory cannot be found, when sure enough the .txt is in there.

    This code used to work, and now somehow it doesnt:
    $.evalFile(‘Expressions.txt’)

  4. Harry says:

    So, you have a txt doc at the root of your hard drive (not in a folder) ?

    Also, make sure you are not using smart quotes, but just “regular quotes” or even ‘ single quotes ‘

  5. Alex says:

    I wish this could be more portable, with relative folder referencing…

  6. David says:

    I tried the expression, after replacing all the quotes which by the way looked exactly the same after I replaced them and I’m only getting “Not found”.

    I already tried changing the name of my comp and variables on my document to different random things and nothing has worked so far, the only difference I have on my document is that it is an .rtf, not a .txt file, could that be affecting the result?

    Best,

    D.

  7. Harry says:

    Do NOT use an RTF. This file has additional formatting in it that will cause errors. The text file needs to include only the expression.

    I also updated the text here in the post to remove all its formatting. Try copying that again, and use a plain txt doc. I just tried it here and it seems to be working.

  8. Ryan Ragle says:

    This is a great tutorial! I am developing a project that will be translated into 14 languages and want to have as little translation costs as possible. I took it one step further and made a comp to hold the variables incase another agency has to make modifications and they don’t want to try to recreate my path structure:

    Step 1 – Create a new Comp in your project, named “DataTranslationLocation”
    Step 2 – Create text fields and name them “PCPath”, “MacPath” and “DataFile”
    Step 3 – Enter your data into each field i.e. PCPath source text will be something like “file://c:\\\\Expressions\”
    Step 4 – Use this modification from above posts:
    try {
    if ($.os.indexOf(“Mac”) != -1)
    myPath = comp(“DataTranslationLocation”).layer(“MacPath”).text.sourceText;
    else
    myPath = comp(“DataTranslationLocation”).layer(“PCPath”).text.sourceText;
    myPath += comp(“DataTranslationLocation”).layer(“DataFile”).text.sourceText;
    $.evalFile (myPath);
    eval(thisComp.name);
    }
    catch(err){“Not found: ” + myPath};

    This way if the project gets handed to someone else, they won’t have to create some arbitrary directory structure for the 50 compositions you created, they’ll just change the files in the comp named “DataTranslationLocation”.

    Of course if there is a better way to handle global variables in after effects, I’m all ears :)

  9. Ryan Ragle says:

    One last nit on my previous post: error statement would be a little more accurate like this:

    catch(err){“Path: ” + myPath + “\r” + err.toString()};

  10. Dave says:

    Thank you so much for this. I’ve been looking for a way to reuse expressions simply since AE5.5. Somehow I never even stumbled on #include (and of course it’s not in any documentation). I haven’t had a chance to try this solution, but I’m very much looking forward to it.

    In reference to Ryan Ragle’s question about global variables that can contain arbitrary text, the best way I’ve found is not to use text layers, but to have a “globals” composition with a bunch of null or solid layers, each with a simple slider effect in it. Then you reference a variable like this:

    comp(“globals”).layer(“var1″).effect(1).name;

    One advantage of this is that you can have arrays this way, by just adding more slider effects to a layer. And you can even have some rudimentary data structures by using the slider values as well. I also find it easier to rename sliders than to fill in text fields, which requires using the canvas.

    So far I haven’t run into any special characters which are disallowed in slider names. Certainly it’ll work fine for filenames.

  11. nikits says:

    Pc/Ae cs4/data.txt on c:/Expressions

    syntax error
    expression disabled
    error occured at line 2
    comp: ‘Line1′
    property: source text

    try{
    myPath = “file://c:\\\\Expressions\\”;
    myPath += “data.txt”;
    $.evalFile (myPath);
    eval(thisComp.name);
    }catch(err){”Not Found”}

  12. [...] How to: setting up a extrenal text document [...]

  13. Merci pour cette article, un info utile merci ,

  14. kunda says:

    i can’t download a preformatted text document and spreadsheet cuz’ the kink is over i think

  15. Shawn says:

    I’ve been working on this all day. Here is the code I have for my Windows 7 platform on AE CS4:

    myPath = ‘C:\Expressions\data.txt’;
    $.evalFile (myPath);
    eval(thisComp.name);

    I keep getting an error saying folder or file not found, even though this is the exact path. I’ve tried a few variations, including one that was almost exactly the prescribed code, but I get the same error.

    Any ideas why?

    • admin says:

      for some reason, the path in Windows requires two slashes \\ for each \. Why? I dunno. In your case, I think there should be “2″, which would result in needing 4. Try that?

  16. Shawn says:

    Perfect. Thanks!

  17. pw says:

    I get this error when attempting to duplicate the layer and add to the text file:

    “After Effects warning: Expected: ; Expression disabled.

    Error occured at line 3.
    Comp: ‘Open’
    Layer: 1 (”)
    Property: ‘Source Text’

    My file looks like this:

    Open = ‘Test 1′

    Open 2 = ‘Test 2′
    ;

    • admin says:

      “Open 2″ is not a variable. It sees you saying “Open” and then “2=..” which does not make sense. Also, do not use smart quotes. You have ′ instead of ‘

  18. chromaKiki says:

    On my Win 7 (64 bit) this is how expression works like a charm:

    try {
    myPath = “c:\\Temp\\testText.txt”;
    $.evalFile (myPath);
    eval(thisComp.name)[0];
    }
    catch(err){“Not found: ” + myPath};

    A big Thanks to guys setting this up and sharing.