/*
Changes the source text of selected text layers to user input.
Adds a hold keyframe if the layer has existing keyframes.
Script by Jack Vaughan (jackvaughan.com/tools)
*/
(function() {
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
alert("Please select a composition.");
return;
}
var selectedLayers = comp.selectedLayers;
if (selectedLayers.length === 0) {
alert("Please select at least one text layer.");
return;
}
var newText = prompt("Enter new text:", "");
if (newText === null) {
// User canceled the prompt
return;
}
app.beginUndoGroup("Replace Text");
for (var i = 0; i < selectedLayers.length; i++) {
var layer = selectedLayers[i];
var sourceTextProp = layer.property("Source Text");
if (sourceTextProp instanceof Property) {
if (sourceTextProp.numKeys > 0) {
// Add a hold keyframe at the current time
sourceTextProp.setValueAtTime(comp.time, newText);
sourceTextProp.setInterpolationTypeAtKey(
sourceTextProp.numKeys,
KeyframeInterpolationType.HOLD
);
} else {
// Set the source text without adding a keyframe
sourceTextProp.setValue(newText);
}
} else {
alert("Layer \"" + layer.name + "\" is not a text layer.");
}
}
app.endUndoGroup();
})();