This script automates the process of creating multiple compositions in After Effects from a single template. Each line of text that you input will become its own composition. The script first validates that your project is open and then displays a user-friendly dialog for you to enter your text. It then searches your project for a composition named “Template” and, within that composition, locates a text layer also named “Template”. For every valid line of text provided, the script duplicates the template comp, renames it using the first few words of the text (removing any special characters), and updates the text layer with your sentence. All newly created compositions are organized in a folder called “Generated Comps”.
JavaScript
// Text to Multiple Comps
// This script creates multiple compositions from a template,
// using each line of text as a separate composition
(function() {
// Helper function to trim whitespace since ExtendScript doesn't have String.trim()
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
// Check if a project is open
if (app.project === null) {
alert("Please open a project first.");
return;
}
// Create the main UI window
var mainWindow = new Window("dialog", "Text to Multiple Comps");
mainWindow.orientation = "column";
mainWindow.alignChildren = ["center", "top"];
mainWindow.spacing = 10;
mainWindow.margins = 16;
// Add group for text input
var textGroup = mainWindow.add("group");
textGroup.orientation = "column";
textGroup.alignChildren = ["left", "top"];
textGroup.spacing = 10;
textGroup.margins = 0;
// Add label
textGroup.add("statictext", undefined, "Enter your sentences (one per line):");
// Add multiline edittext
var textInput = textGroup.add("edittext", undefined, "", {multiline: true, scrolling: true});
textInput.preferredSize.width = 400;
textInput.preferredSize.height = 200;
// Add buttons group
var buttonGroup = mainWindow.add("group");
buttonGroup.orientation = "row";
buttonGroup.alignChildren = ["center", "center"];
buttonGroup.spacing = 10;
buttonGroup.margins = 0;
var createButton = buttonGroup.add("button", undefined, "Create Comps");
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
// Button click handlers
cancelButton.onClick = function() {
mainWindow.close();
};
createButton.onClick = function() {
app.beginUndoGroup("Create Comps from Text");
try {
var sentences = textInput.text.split("\n").filter(function(sentence) {
return trim(sentence) !== "";
});
if (sentences.length === 0) {
alert("Please enter at least one sentence.");
return;
}
// Find template comp
var templateComp = null;
for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem && app.project.item(i).name === "Template") {
templateComp = app.project.item(i);
break;
}
}
if (templateComp === null) {
alert("Cannot find composition named 'Template'");
return;
}
// Find text layer named "Template" in the template comp
var textLayer = null;
for (var j = 1; j <= templateComp.numLayers; j++) {
if (templateComp.layer(j) instanceof TextLayer && templateComp.layer(j).name === "Template") {
textLayer = templateComp.layer(j);
break;
}
}
if (textLayer === null) {
alert("Cannot find text layer named 'Template' in the Template composition");
return;
}
// Create new folder for generated comps
var outputFolder = app.project.items.addFolder("Generated Comps");
// Process each sentence
for (var k = 0; k < sentences.length; k++) {
var sentence = trim(sentences[k]);
if (sentence === "") continue;
// Create comp name from first three words
var words = sentence.split(" ");
var compName = words.slice(0, Math.min(3, words.length)).join("_");
compName = compName.replace(/[^\w\s-]/g, ""); // Remove special characters
// Duplicate template comp
var newComp = templateComp.duplicate();
newComp.parentFolder = outputFolder;
newComp.name = compName;
// Find and update text layer in new comp
var newTextLayer = null;
for (var l = 1; l <= newComp.numLayers; l++) {
if (newComp.layer(l) instanceof TextLayer && newComp.layer(l).name === "Template") {
newTextLayer = newComp.layer(l);
break;
}
}
if (newTextLayer) {
// Update text content
newTextLayer.property("Source Text").setValue(sentence);
}
}
alert("Created " + sentences.length + " compositions successfully!");
mainWindow.close();
} catch (error) {
alert("An error occurred: " + error.toString());
}
app.endUndoGroup();
};
mainWindow.center();
mainWindow.show();
})();
- Prerequisites:
- Ensure that your project is open in After Effects.
- Ensure there is a composition named “Template” that contains a text layer also named “Template”. This composition serves as the base for all generated compositions.
- Running the Script:
- Open the ExtendScript Toolkit or your preferred script editor for After Effects and run the script.
- A dialog window titled “Text to Multi-Comps” will appear.
- Entering Your Text:
- In the displayed dialog, you will see a multiline text input field.
- Enter your sentences, placing each sentence on a new line. These will be used to create individual compositions.
- Creating the Compositions:
- Click the “Create Comps” button in the dialog.
- The script will process your input by trimming any unnecessary whitespace and filtering out empty lines.
- For each non-empty sentence, the script will: • Duplicate the “Template” composition. • Generate a new name for the duplicated comp using the first few words of the sentence (special characters will be removed). • Update the text layer within the new composition to reflect the sentence. • Group all generated compositions in a folder titled “Generated Comps” for easy organization.
- Completing the Process:
- Once all sentences have been processed, you will receive a confirmation alert indicating the number of compositions created.
- If you decide to cancel the operation at any time, simply click the “Cancel” button in the dialog, and no changes will be made.