Dynamic Typing with Source Text in After Effects

Automating text animation in Adobe After Effects using expressions provides a powerful alternative to manual keyframing. By targeting the sourceText property via expressions, you can programmatically read, manipulate, and generate string content on the fly. This article covers the fundamental mechanics of accessing sourceText through expressions and demonstrates how to build a fully automated dynamic typing (typewriter) effect without relying on standard text animators.

Understanding Expression Access to Source Text

In Adobe After Effects, every text layer possesses a Source Text property located under the layer's Text group. In the JavaScript expression engine, this property is represented as:

text.sourceText

Accessing text.sourceText returns the string value of the layer as an object. When evaluated within expressions, you can manipulate this value using standard JavaScript string methods such as .length, .substring(), .slice(), and .toUpperCase().

Beyond reading the layer's own content, you can reference the sourceText of other layers or compositions. For example, linking Layer B to Layer A's text:

thisComp.layer("Layer A").text.sourceText

Expression access allows you to treat text as dynamic data. You can dynamically inject variables, format numerical counters, link text across multiple compositions, or feed text from external data files like JSON or CSV.

Setting Up Dynamic Typing via Expressions

A dynamic typing effect displays characters sequentially over time, mimicking someone typing on a keyboard. While After Effects includes built-in Typewriter presets, using expressions allows the animation speed to adapt dynamically to text length changes or keyframe-driven slider controls.

Method 1: Slider-Controlled Dynamic Typing

The most flexible approach uses a Slider Control to dictate exactly how many characters are displayed at any given moment.

  1. Select your text layer and type out your complete phrase.
  2. Go to Effect > Transition > Slider Control to add a slider to the text layer. Rename this effect to Type Speed.
  3. Twirl open the layer properties to find Text > Source Text.
  4. Alt-click (Option-click on macOS) the stopwatch next to Source Text to open the expression field.
  5. Enter the following expression:
const slider = effect("Type Speed")("Slider");
const fullText = value;
const charCount = Math.round(slider);

fullText.substring(0, charCount);
  1. Set a keyframe on the slider with a value of 0 where you want the typing to start, and another keyframe set to the length of the string (or simply 100) where you want it to finish.

Method 2: Fully Automated Time-Based Typing

If you want the text to type automatically based on project time without setting keyframes, you can calculate the character count using time and a speed variable.

Apply this expression to the Source Text property:

const fullText = value;
const charactersPerSecond = 15;
const startTime = 1.0; // Time in seconds when typing begins

if (time > startTime) {
    const elapsed = time - startTime;
    const charCount = Math.floor(elapsed * charactersPerSecond);
    fullText.substring(0, charCount);
} else {
    "";
}

This script evaluates how many seconds have passed since startTime, multiplies that duration by the typing rate, and reveals the corresponding slice of the text string.

Adding a Dynamic Blinking Cursor

To enhance realism, you can append a blinking cursor (| or _) to the dynamically typed text:

const fullText = value;
const speed = 15;
const startTime = 1.0;
const cursorChar = "|";
const blinkRate = 2; // Blinks per second

let currentText = "";

if (time > startTime) {
    const elapsed = time - startTime;
    const charCount = Math.floor(elapsed * speed);
    currentText = fullText.substring(0, charCount);
}

// Cursor blinking logic
const showCursor = Math.floor(time * blinkRate * 2) % 2 === 0;

if (showCursor && currentText.length < fullText.length) {
    currentText + cursorChar;
} else {
    currentText;
}

This expression isolates character rendering to programmatic time increments while calculating an alternating on/off state for the cursor character until the final string is fully revealed.