Skip to main content
daitranthanhoa
Inspiring
July 19, 2022
Answered

Why can't replace value of sourceText?

How can replace a new text to sourceText of Layer?

I had try this code, but it not allow:

 

var firstComp = app.project.item(4);
for(var iLayer=1;iLayer<=firstComp.numLayers;iLayer ++)
{
var firstLayer = firstComp.layer(iLayer);

if( firstLayer.sourceText!=undefined && firstLayer.sourceText.numKeys >0 )
{
   //firstLayer.sourceText.value="abc";
    firstLayer.sourceText.setValue( "abc");
}
}

This topic is closed to new replies. Start a new post to keep the conversation going.
Dan Ebberts
Dan EbbertsCorrect answer
Community Expert
July 20, 2022

If you want to replace the text at a particular key, you'd need to use keyValue() to retrieve the current value, and setValueAtKey() to replace the value at that key. So if you wanted, for example, to replace the text at the first keyframe, it might look like this:

var comp = app.project.activeItem;
var layer;
var sourceText;
var textDoc;
for (var i = 1; i <= comp.numLayers; i++){
	layer = comp.layer(i);
	 if (layer instanceof TextLayer){
		 sourceText = layer.property("Text").property("Source Text");
		 textDoc = sourceText.keyValue(1);
		 textDoc.text = "abc";
		 sourceText.setValueAtKey(1,textDoc);
	}
}

3 replies

Dan Ebberts
Community Expert
Community Expert
July 19, 2022

Try this:

var comp = app.project.activeItem;
var layer;
var sourceText;
var textDoc;
for (var i = 1; i <= comp.numLayers; i++){
	layer = comp.layer(i);
	 if (layer instanceof TextLayer){
		 sourceText = layer.property("Text").property("Source Text");
		 textDoc = sourceText.value;
		 textDoc.text = "abc";
		 sourceText.setValue(textDoc);
	}
}
Translate
daitranthanhoa
Inspiring
July 20, 2022

It only allow set value when sourceText.numKeys  =0,

If sourceText.numKeys >0 , can't replace new text.

Translate
Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
July 20, 2022

If you want to replace the text at a particular key, you'd need to use keyValue() to retrieve the current value, and setValueAtKey() to replace the value at that key. So if you wanted, for example, to replace the text at the first keyframe, it might look like this:

var comp = app.project.activeItem;
var layer;
var sourceText;
var textDoc;
for (var i = 1; i <= comp.numLayers; i++){
	layer = comp.layer(i);
	 if (layer instanceof TextLayer){
		 sourceText = layer.property("Text").property("Source Text");
		 textDoc = sourceText.keyValue(1);
		 textDoc.text = "abc";
		 sourceText.setValueAtKey(1,textDoc);
	}
}
Translate